package com.monitor.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.monitor.dao.*;
import com.monitor.entity.*;
import com.monitor.service.*;
import com.monitor.util.BlankUtil;
import com.monitor.util.DataUtil;
import com.monitor.util.IDUtils;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Transactional
@RequestMapping("/erpXiaoShouWeiXiu")
@Api(description = "销售/维修")
public class ErpWeiXiuController {

//    @Resource
//    private ErpSaleRepairRepairMapper erpSaleRepairRepairMapper;


    @Resource
    private ErpSystemParametersChildService erpSystemParametersChildService;

    @Resource
    private ErpSystemMessageService erpSystemMessageService;
    @Resource
    private ErpSaleRepairOrderService erpSaleRepairOrderService;        //销售维修表
    @Resource
    private ErpSaleRepairSaleService erpSaleRepairSaleService;  //销售维修-销售表
    @Resource
    private ErpCustomerWarehouseService erpCustomerWarehouseService; //客户仓库service
    @Resource
    private ErpSaleRepairRepairService erpSaleRepairRepairService;   //销售维修-轮胎维修表
    @Resource
    private ErpSaleRepairGarageService erpSaleRepairGarageService;//销售维修-车辆维修表
    @Resource
    private ErpInsuranceService erpInsuranceService; //保险单号
    @Resource
    private ErpHcService erpHcService;        //耗材明细
    @Resource
    private ErpSaleRepairSuppliesService erpSaleRepairSuppliesService;  //物料维修维修耗材

    @Resource
    private ErpContractRepairService repairService;
    @Resource
    private ErpMaterielService erpMaterielService;//物料表

    //    @Resource
//    private ErpMaterielMapper erpMaterielMapper;//物料表
    @Resource
    private ErpWarehouseService erpWarehouseService;//物料仓库表
    @Resource
    private ErpTireArchivesWorkService erpTireArchivesWorkService;   //轮胎档案工作表
    @Resource
    private ErpCustomerCarMapper erpCustomerCarMapper;
    @Resource
    private ErpCarTireArchivesService erpCarTireArchivesService;//车辆轮胎档案表 service 关联车辆轮胎传感器轮辋的
    @Resource
    private ErpTireSensorService erpTireSensorService; //传感器定位器,轮胎关联service

    @Resource
    private ErpManagementService erpManagementService; //传感器定位器,轮胎关联service

    @Resource
    private ErpBidataMapper erpBidataMapper;

    @Resource
    private ErpBidataListMapper erpBidataListMapper;

    @Resource
    private ErpUserService erpUserService;

    @Resource
    private ErpSheetIdService erpSheetIdService;

    @Resource
    private ErpCustomerService erpCustomerService;

    @Resource
    private ErpMaterielFlowSummaryService erpMaterielFlowSummaryService;


    @RequestMapping("/xiaoshouweixiuShangPinLieBiao")
    @ApiOperation("销售维修-商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carPai", value = "车牌号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpSaleRepairSale>> xiaoshouweixiuShangPinLieBiao(String carPai) {

        Result<List<ErpSaleRepairSale>> result = new Result<List<ErpSaleRepairSale>>();
        try {
//            ErpSaleRepairOrder carPlate = erpSaleRepairOrderService.selectOne(new EntityWrapper<ErpSaleRepairOrder>().eq("car_plate", carPai));
//            List<ErpSaleRepairSale> order_id = erpSaleRepairSaleService.selectList(new EntityWrapper<ErpSaleRepairSale>().eq("order_id", carPlate.getId()));
//            for (int i = 0; i < order_id.size(); i++) {
//                ErpSaleRepairSale erpSaleRepairSale =  order_id.get(i);
//                ErpCustomerWarehouse warehouse_id = erpCustomerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("id", erpSaleRepairSale.getWarehouseId()));
//                erpSaleRepairSale.setWarehouseName(warehouse_id.getWarName());
//            }
//                result.setData(order_id);
//            int order_id1 = erpSaleRepairSaleService.selectCount(new EntityWrapper<ErpSaleRepairSale>().eq("order_id", carPlate.getId()));
//            result.setCount(order_id1);

            List<ErpSaleRepairSale> car_pai1 = new ArrayList<>();
            List<ErpSaleRepairSale> car_pai = erpSaleRepairSaleService.selectList(new EntityWrapper<ErpSaleRepairSale>().eq("car_pai", carPai));
            for (int i = 0; i < car_pai.size(); i++) {
                ErpSaleRepairSale erpSaleRepairSale = car_pai.get(i);
                ErpCustomerWarehouse erpCustomerWarehouse = erpCustomerWarehouseService.selectById(erpSaleRepairSale.getWarehouseId());
                erpSaleRepairSale.setWarehouseName(erpCustomerWarehouse.getWarName());
                if (erpSaleRepairSale.getOrderId().equals("1")) {
                    car_pai1.add(erpSaleRepairSale);
                }
            }
            result.setData(car_pai1);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/deletexiaoshouweixiuShangPinLieBiao")
    @ApiOperation("删除-商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品列表id", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> deletexiaoshouweixiuShangPinLieBiao(Integer id) {

        Result<Integer> result = new Result<>();
        try {
//            ErpSaleRepairOrder carPlate = erpSaleRepairOrderService.selectOne(new EntityWrapper<ErpSaleRepairOrder>().eq("car_plate", carPai));
//            List<ErpSaleRepairSale> order_id = erpSaleRepairSaleService.selectList(new EntityWrapper<ErpSaleRepairSale>().eq("order_id", carPlate.getId()));
//            for (int i = 0; i < order_id.size(); i++) {
//                ErpSaleRepairSale erpSaleRepairSale =  order_id.get(i);
//                ErpCustomerWarehouse warehouse_id = erpCustomerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("id", erpSaleRepairSale.getWarehouseId()));
//                erpSaleRepairSale.setWarehouseName(warehouse_id.getWarName());
//            }
//                result.setData(order_id);
//            int order_id1 = erpSaleRepairSaleService.selectCount(new EntityWrapper<ErpSaleRepairSale>().eq("order_id", carPlate.getId()));
//            result.setCount(order_id1);
            ///11111

            ErpSaleRepairSale erpSaleRepairSale = erpSaleRepairSaleService.selectById(id);
            String[] groupIds = erpSaleRepairSale.getMaterielOutTo().split("-");//把上面的去向截取
            ErpCustomerCar cusCars = erpCustomerCarMapper.selectByPlate(groupIds[0]);
            ErpCarTireArchives car_ids = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCars.getId()).eq("tire_location", groupIds[1]));//.eq("car_id", cusCar.getId()
            ErpMateriel erpMateriel2 = erpMaterielService.selectById(car_ids.getTireId());

            List<ErpSaleRepairRepair> list = erpSaleRepairRepairService.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("shop_shou", id));
            list.parallelStream().forEach(aaa -> {

                if (aaa.getState() == 0) {
                    erpMateriel2.setMaterielBrand(aaa.getMaterielBrand());
                    erpMateriel2.setMaterielModel(aaa.getMaterielModel());
                    erpMateriel2.setMaterielName(aaa.getMaterielName());
                    erpMateriel2.setMaterielSpecifications(aaa.getMaterielSpecifications());
                    erpMateriel2.setMaterielModel(aaa.getMaterielModel());
                    erpMateriel2.setMaterielPlace(aaa.getMaterielPlace());
                    erpMateriel2.setMaterielState(aaa.getMaterielState());
                    erpMateriel2.setMaterielUnit(aaa.getMaterielUnit());
                    erpMateriel2.setMaterielProductionId(aaa.getMaterielProductionId());
                    erpMateriel2.setMaterielNumber(aaa.getMaterielNumber());
                    erpMateriel2.setDelectType(0);
                    erpMaterielService.updateById(erpMateriel2);
                    erpSaleRepairRepairService.deleteById(aaa.getId());
                } else if (aaa.getState() == 1
                        ) {
                    ErpMateriel erpCustomerWarehouse = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("materiel_production_id", aaa.getMaterielProductionId()).eq("materiel_name", aaa.getMaterielName()).eq("materiel_brand", aaa.getMaterielBrand()).eq("materiel_Specifications", aaa.getMaterielSpecifications())
                            .eq("materiel_place", aaa.getMaterielPlace())
                            .eq("materiel_state", aaa.getMaterielState())
                            .eq("materiel_unit", aaa.getMaterielUnit()).eq("materiel_production_id", aaa.getMaterielProductionId()));
                    if (erpCustomerWarehouse != null) {
                        erpCustomerWarehouse.setDelectType(1);
                        erpMaterielService.updateById(erpCustomerWarehouse);
                        erpSaleRepairRepairService.deleteById(aaa.getId());
                    }
                }
            });
            result.setData(erpSaleRepairSaleService.deleteById(id) ? 1 : 0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    @RequestMapping("/xiaoshouweixiuShangPinLieBiaoByOrder")
    @ApiOperation("销售维修-商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "订单号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpSaleRepairSale>> xiaoshouweixiuShangPinLieBiaoByOrder(String order) {

        Result<List<ErpSaleRepairSale>> result = new Result<List<ErpSaleRepairSale>>();
        try {
//            ErpSaleRepairOrder carPlate = erpSaleRepairOrderService.selectOne(new EntityWrapper<ErpSaleRepairOrder>().eq("car_plate", carPai));
//            List<ErpSaleRepairSale> order_id = erpSaleRepairSaleService.selectList(new EntityWrapper<ErpSaleRepairSale>().eq("order_id", carPlate.getId()));
//            for (int i = 0; i < order_id.size(); i++) {
//                ErpSaleRepairSale erpSaleRepairSale =  order_id.get(i);
//                ErpCustomerWarehouse warehouse_id = erpCustomerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("id", erpSaleRepairSale.getWarehouseId()));
//                erpSaleRepairSale.setWarehouseName(warehouse_id.getWarName());
//            }
//                result.setData(order_id);
//            int order_id1 = erpSaleRepairSaleService.selectCount(new EntityWrapper<ErpSaleRepairSale>().eq("order_id", carPlate.getId()));
//            result.setCount(order_id1);
            List<ErpSaleRepairSale> car_pai = erpSaleRepairSaleService.selectList(new EntityWrapper<ErpSaleRepairSale>().eq("order_id", order));
            result.setData(car_pai);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    @RequestMapping("/xiaoshouweixiuLunTaiWeiXiu")
    @ApiOperation("销售维修轮胎维修查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carPai", value = "车牌号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Map<String, Object>> xiaoshouweixiuLunTaiWeiXiu(String carPai) {

        Result<Map<String, Object>> result = new Result<Map<String, Object>>();
        try {
            ErpSaleRepairOrder carPlate = erpSaleRepairOrderService.selectOne(new EntityWrapper<ErpSaleRepairOrder>().eq("car_plate", carPai));
            List<ErpSaleRepairRepair> order_id = erpSaleRepairRepairService.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("order_id", carPlate.getId()).eq("state", 0));
            List<ErpSaleRepairRepair> order_id1 = erpSaleRepairRepairService.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("order_id", carPlate.getId()).eq("state", 1));
            Map<String, Object> map = new HashMap<>();
            map.put("weiwancheng", order_id);
            map.put("yiwancheng", order_id1);
            result.setData(map);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/xiaoshouweixiuLunTaiWeiXiuByOrder")
    @ApiOperation("销售维修轮胎维修查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "订单号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Map<String, Object>> xiaoshouweixiuLunTaiWeiXiuByOrder(String order) {

        Result<Map<String, Object>> result = new Result<Map<String, Object>>();
        try {
            ErpSaleRepairOrder carPlate = erpSaleRepairOrderService.selectOne(new EntityWrapper<ErpSaleRepairOrder>().eq("sale_repair_sheet_id", order));
            List<ErpSaleRepairRepair> order_id = erpSaleRepairRepairService.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("order_id", carPlate.getId()).eq("state", 0));
            List<ErpSaleRepairRepair> order_id1 = erpSaleRepairRepairService.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("order_id", carPlate.getId()).eq("state", 1));
            Map<String, Object> map = new HashMap<>();
            map.put("weiwancheng", order_id);
            map.put("yiwancheng", order_id1);
            result.setData(map);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/xiaoshouweixiuByOrder")
    @ApiOperation("销售维修总表大页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "订单号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpSaleRepairOrder> xiaoshouweixiuByOrder(String order) {

        Result<ErpSaleRepairOrder> result = new Result<>();
        try {
            ErpSaleRepairOrder carPlate = erpSaleRepairOrderService.selectOne(new EntityWrapper<ErpSaleRepairOrder>().eq("sale_repair_sheet_id", order));
            result.setData(carPlate);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/xiaoshouweixiuLunTaiWeiXiusByOrder")
    @ApiOperation("销售维修轮胎维修查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "订单号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Map<String, Object>> xiaoshouweixiuLunTaiWeiXiusByOrder(String order) {

        Result<Map<String, Object>> result = new Result<Map<String, Object>>();
        try {
            List<ErpSaleRepairRepair> order_id = erpSaleRepairRepairService.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("order_id", order).eq("state", 0));
            List<ErpSaleRepairRepair> order_id1 = erpSaleRepairRepairService.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("order_id", order).eq("state", 1));
            Map<String, Object> map = new HashMap<>();
            map.put("weiwancheng", order_id);
            map.put("yiwancheng", order_id1);
            result.setData(map);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/xiaoshouweixiuLunTaiWeiXius")
    @ApiOperation("销售维修轮胎维修查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carPai", value = "车牌号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Map<String, Object>> xiaoshouweixiuLunTaiWeiXius(String carPai) {

        Result<Map<String, Object>> result = new Result<Map<String, Object>>();
        List<ErpSaleRepairRepair> order_ids = new ArrayList<>();
        List<ErpSaleRepairRepair> order_ids1 = new ArrayList<>();
        try {

            List<ErpSaleRepairRepair> order_id = erpSaleRepairRepairService.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("car_plate", carPai).eq("state", 0));
            order_id.parallelStream().forEach(order -> {

                        order_ids.add(order);

                    }
            );

            List<ErpSaleRepairRepair> order_id1 = erpSaleRepairRepairService.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("car_plate", carPai).eq("state", 1));
            order_id1.parallelStream().forEach(orders -> { //1
                if (orders.getOrderId() == null) {
                    order_ids1.add(orders);
                }
            });
            Map<String, Object> map = new HashMap<>();
            map.put("weiwancheng", order_ids);
            map.put("yiwancheng", order_ids1);
            result.setData(map);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/gongzuohuizongdetil")
    @ApiOperation("工作汇总详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carPai", value = "工单号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Map<String, Object>> gongzuohuizongdetil(String carPai) {

        Result<Map<String, Object>> result = new Result<Map<String, Object>>();

        try {

            List<ErpSaleRepairRepair> order_id = erpSaleRepairRepairService.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("order_id", carPai));


            List<ErpSaleRepairSale> car_pai1 = new ArrayList<>();
            List<ErpSaleRepairSale> car_pai = erpSaleRepairSaleService.selectList(new EntityWrapper<ErpSaleRepairSale>().eq("order_id", carPai));
            for (int i = 0; i < car_pai.size(); i++) {
                ErpSaleRepairSale erpSaleRepairSale = car_pai.get(i);
                ErpCustomerWarehouse erpCustomerWarehouse = erpCustomerWarehouseService.selectById(erpSaleRepairSale.getWarehouseId());
                erpSaleRepairSale.setWarehouseName(erpCustomerWarehouse.getWarName());
                car_pai1.add(erpSaleRepairSale);
            }

            List<ErpManagement> erpManagementList = erpManagementService.selectList(new EntityWrapper<ErpManagement>().eq("work_id", carPai));
            List<ErpSaleRepairOrder> erpSaleRepairOrderList = erpSaleRepairOrderService.selectList(new EntityWrapper<ErpSaleRepairOrder>().eq("sale_repair_sheet_id", carPai));
            Map<String, Object> map = new HashMap<>();
            map.put("wx", car_pai1);
            map.put("xsyiwancheng", order_id);
            map.put("zong", erpManagementList);
            map.put("erpSaleRepairOrder", erpSaleRepairOrderList);
            result.setData(map);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/deleteXiaoshouweixiuLunTaiWeiXius")
    @ApiOperation("删除销售维修轮胎维修")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carPai", value = "车牌号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> xiaoshouweixiuLunTaiWeiXius(Integer id) {

        Result<Integer> result = new Result<>();
        try {

            ErpSaleRepairRepair erpSaleRepairRepair = erpSaleRepairRepairService.selectById(id);
            ErpMateriel erpMateriel = erpMaterielService.selectById(erpSaleRepairRepair.getErpMaterielId());
            if (erpSaleRepairRepair.getShopShou() != null) {
                erpSaleRepairSaleService.deleteById(erpSaleRepairRepair.getShopShou());
            }
//            ErpMateriel erpMateriel = erpMaterielService.selectById()
//            );

            erpMateriel.setMaterielNumber(erpMateriel.getMaterielNumber() + erpSaleRepairRepair.getMaterielNumber());
            erpMaterielService.updateById(erpMateriel);
            result.setData(erpSaleRepairRepairService.deleteById(id) ? 1 : 0);
            result.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping("/xiaoshouweixiuCheLiangWeiXiu")
    @ApiOperation("销售维修车辆维修查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carPai", value = "车牌号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpSaleRepairGarage>> xiaoshouweixiuCheLiangWeiXiu(String carPai) {

        Result<List<ErpSaleRepairGarage>> result = new Result<List<ErpSaleRepairGarage>>();
        carPai = carPai.replace(" ", "");

        try {
            ErpSaleRepairOrder carPlate = erpSaleRepairOrderService.selectOne(new EntityWrapper<ErpSaleRepairOrder>().eq("car_plate", carPai));
            List<ErpSaleRepairGarage> order_id = erpSaleRepairGarageService.selectList(new EntityWrapper<ErpSaleRepairGarage>().eq("order_id", carPlate.getId()));
            ErpInsurance car_number = erpInsuranceService.selectOne(new EntityWrapper<ErpInsurance>().eq("car_number", carPai));
            for (int i = 0; i < order_id.size(); i++) {
                ErpSaleRepairGarage erpSaleRepairGarage = order_id.get(i);
                if (null != car_number) {
                    erpSaleRepairGarage.setInsuranceCompany(car_number.getInsuranceCompany());
                    erpSaleRepairGarage.setInsurancepolicyno(car_number.getInsurancepolicyno());
                }
            }
            result.setData(order_id);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/xiaoshouweixiuCheLiangWeiXiuByOrder")
    @ApiOperation("销售维修车辆维修查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "订单号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpSaleRepairGarage>> xiaoshouweixiuCheLiangWeiXiuByOrder(String order) {

        Result<List<ErpSaleRepairGarage>> result = new Result<List<ErpSaleRepairGarage>>();

        try {
            ErpSaleRepairOrder carPlate = erpSaleRepairOrderService.selectOne(new EntityWrapper<ErpSaleRepairOrder>().eq("sale_repair_sheet_id", order));
            List<ErpSaleRepairGarage> order_id = erpSaleRepairGarageService.selectList(new EntityWrapper<ErpSaleRepairGarage>().eq("order_id", carPlate.getId()));
            ErpInsurance car_number = erpInsuranceService.selectOne(new EntityWrapper<ErpInsurance>().eq("car_number", carPlate.getCarPlate()));
            for (int i = 0; i < order_id.size(); i++) {
                ErpSaleRepairGarage erpSaleRepairGarage = order_id.get(i);
                if (null != car_number) {
                    erpSaleRepairGarage.setInsuranceCompany(car_number.getInsuranceCompany());
                    erpSaleRepairGarage.setInsurancepolicyno(car_number.getInsurancepolicyno());
                }
            }
            result.setData(order_id);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/xiaoshouweixiuCheLiangWeiXius")
    @ApiOperation("销售维修车辆维修查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carPai", value = "车牌号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpSaleRepairGarage>> xiaoshouweixiuCheLiangWeiXius(String carPai) {

        Result<List<ErpSaleRepairGarage>> result = new Result<List<ErpSaleRepairGarage>>();
        carPai = carPai.replace(" ", "");
        List<ErpSaleRepairGarage> order_ids = new ArrayList<>();
        try {
            List<ErpSaleRepairGarage> order_id = erpSaleRepairGarageService.selectList(new EntityWrapper<ErpSaleRepairGarage>().eq("car_pai", carPai));
            order_id.parallelStream().forEach(order -> {
                if (order.getOrderId() == null) {
                    order_ids.add(order);
                }
            });

            result.setData(order_ids);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping("/gongzuohuizong_cheliangwuliao")
    @ApiOperation("查询工作汇总的车辆物料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderid", value = "工作汇总单号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpSaleRepairGarage>> gongzuohuizong_cheliangwuliao(String orderid) {

        Result<List<ErpSaleRepairGarage>> result = new Result<List<ErpSaleRepairGarage>>();
        orderid = orderid.replace(" ", "");
        List<ErpSaleRepairGarage> order_ids = new ArrayList<>();
        try {
            List<ErpSaleRepairGarage> order_id = erpSaleRepairGarageService.selectList(new EntityWrapper<ErpSaleRepairGarage>().eq("order_id", orderid));
            order_id.parallelStream().forEach(order -> {

                order_ids.add(order);

            });

            result.setData(order_ids);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/xiaoshouweixiuCheLiangWeiXiusByOrder")
    @ApiOperation("销售维修车辆维修查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "订单号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpSaleRepairGarage>> xiaoshouweixiuCheLiangWeiXiusByOrder(String order) {

        Result<List<ErpSaleRepairGarage>> result = new Result<List<ErpSaleRepairGarage>>();

        try {
            List<ErpSaleRepairGarage> order_id = erpSaleRepairGarageService.selectList(new EntityWrapper<ErpSaleRepairGarage>().eq("order_id", order));


            result.setData(order_id);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/delectxiaoshouweixiuCheLiangWeiXius")
    @ApiOperation("销售维修车辆维修删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carPai", value = "车牌号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> delectxiaoshouweixiuCheLiangWeiXius(Integer id) {

        Result<Integer> result = new Result<>();


        try {


            result.setData(erpSaleRepairGarageService.deleteById(id) ? 1 : 0);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/xiaoshouweixiuCarLiangWeiXiu")
    @ApiOperation("销售维修车辆维修查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carPai", value = "车牌号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpSaleRepairGarage>> xiaoshouweixiuCarLiangWeiXiu(String carPai) {

        Result<List<ErpSaleRepairGarage>> result = new Result<List<ErpSaleRepairGarage>>();
        try {
            List<ErpSaleRepairGarage> car_pai = erpSaleRepairGarageService.selectList(new EntityWrapper<ErpSaleRepairGarage>().eq("car_pai", carPai));
            result.setData(car_pai);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/xiaoshouweixiuCarLiangWeiXiuByOrder")
    @ApiOperation("销售维修车辆维修查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "订单号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpSaleRepairGarage>> xiaoshouweixiuCarLiangWeiXiuByOrder(String order) {

        Result<List<ErpSaleRepairGarage>> result = new Result<List<ErpSaleRepairGarage>>();
        try {
            List<ErpSaleRepairGarage> car_pai = erpSaleRepairGarageService.selectList(new EntityWrapper<ErpSaleRepairGarage>().eq("order_id", order));
            result.setData(car_pai);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/addWuLiaoWeiXiu")
    @ApiOperation("添加物料维修")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "price", value = "价格", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> addWuLiaoWeiXiu(ErpSaleRepairRepair erpSaleRepairRepair) {
        Result<Integer> result = new Result<Integer>();
        try {

            erpSaleRepairRepairService.insert(erpSaleRepairRepair);
            result.setData(erpSaleRepairRepair.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }


    @RequestMapping("/getHCByWarehouse")
    @ApiOperation("通过来源查维修耗材")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "price", value = "价格", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpSaleRepairSupplies>> getHCByWarehouse(Integer warehouseId) {
        Result<List<ErpSaleRepairSupplies>> result = new Result<>();
        try {
            List<ErpSaleRepairSupplies> pid = erpSaleRepairSuppliesService.selectList(new EntityWrapper<ErpSaleRepairSupplies>().eq("pid", warehouseId));
            result.setData(pid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }

    @RequestMapping("/getHCByWarehouseByCarPlate")
    @ApiOperation("通过车牌查维修耗材")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "price", value = "价格", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpSaleRepairSupplies>> getHCByWarehouseByCarPlate(String carPlate) {
        Result<List<ErpSaleRepairSupplies>> result = new Result<>();
        try {
            List<ErpSaleRepairSupplies> pid = erpSaleRepairSuppliesService.selectList(new EntityWrapper<ErpSaleRepairSupplies>().eq("car_plate", carPlate));
            result.setData(pid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/xiaoshouweixiuAddShangPinLieBiao")
    @ApiOperation("销售维修-添加商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carPai", value = "车牌号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> xiaoshouweixiuAddShangPinLieBiao(ErpSaleRepairSale erpSaleRepairSale) {

        Result<Integer> result = new Result<Integer>();
        try {
            //津KYD178[0]      -左1轮  [1]
            String[] groupIds = erpSaleRepairSale.getMaterielOutTo().split("-");//把上面的去向截取  z获取车牌号   和 去向的位置
            ErpMateriel erpMateriel2 = new ErpMateriel(); // 找到对应换掉的物料   注
//            ErpMateriel erpMaterieljiangchuli = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("warehouse_id", war_name).or().isNull("warehouse_id").
//                    eq("materiel_name", erpSaleRepairSale.getMaterielName()).or().isNull("materiel_name").
//                    eq("materiel_brand", erpSaleRepairSale.getMaterielBrand()).or().isNull("materiel_brand").
//                    eq("materiel_Specifications", erpSaleRepairSale.getMaterielSpecifications()).or().isNull("materiel_Specifications")
//                    .eq("materiel_place", erpSaleRepairSale.getMaterielPlace()).or().isNull("materiel_place")
//                    .eq("materiel_state", erpSaleRepairSale.getMaterielState()).or().isNull("materiel_state")
//                    .eq("materiel_unit", erpSaleRepairSale.getMaterielUnit()).or().isNull("materiel_unit"));
            if ((groupIds[0].equals("客户库存")) == false && (groupIds[0].equals("客户留存")) == false) { //判断是否去车上还是去向库存
                ErpCustomerCar cusCars = erpCustomerCarMapper.selectByPlate(groupIds[0]); // 查询对应的车辆 根据车牌号
                // 查询这个车辆上 某个位置的物料是什么 参数是 groupIds[1]  车上物料位置
                ErpCarTireArchives car_ids = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCars.getId()).eq("tire_location", groupIds[1]));//.eq("car_id", cusCar.getId()
                //查询这个位置上的轮胎  轮胎上的所有物料
                ErpTireSensor tire_id = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("tire_id", car_ids.getTireId()));///111

                //查询 将要替换掉的物料 的轮胎
                if (tire_id != null && erpSaleRepairSale.getMaterielName().contains("轮胎")) {
                    erpMateriel2 = erpMaterielService.selectById(tire_id.getTireId()); //这里的car_ids.getTireId() , 应该改成  tire_id里对应的通过判断

                } else if (tire_id != null && erpSaleRepairSale.getMaterielName().contains("传感器")) {
                    erpMateriel2 = erpMaterielService.selectById(tire_id.getSensorId()); //这里的car_ids.getTireId() , 应该改成  tire_id里对应的通过判断

                } else if (tire_id != null && erpSaleRepairSale.getMaterielName().contains("轮辋")) {
                    erpMateriel2 = erpMaterielService.selectById(tire_id.getRimsId()); //这里的car_ids.getTireId() , 应该改成  tire_id里对应的通过判断

                }


            }
//                erp_tire_archives_work
//                ErpTireArchivesWork tire_location = erpTireArchivesWorkService.selectOne(new EntityWrapper<ErpTireArchivesWork>().eq("tire_location", erpSaleRepairSale.getMaterielOutTo()));


            erpSaleRepairSaleService.insert(erpSaleRepairSale); //添加销售维修单  也就是将要操作当前这个物料卖到哪里 多少钱 用在哪个地方去向哪个
            ErpSaleRepairRepair erpSaleRepairRepair = new ErpSaleRepairRepair(); //维修单  查询对应的 销售的当前物料 对应到轮胎的维修上  erp_sale_repair_repair 轮胎维修表

            erpSaleRepairRepair.setMaterielSource(erpCustomerWarehouseService.selectById(erpSaleRepairSale.getWarehouseId()).getWarName()
            );
            erpSaleRepairRepair.setMaterielBrand(erpSaleRepairSale.getMaterielBrand());
            erpSaleRepairRepair.setMaterielOutTo(erpSaleRepairSale.getMaterielOutTo());
            erpSaleRepairRepair.setMaterielModel(erpSaleRepairSale.getMaterielModel());
//            erpSaleRepairRepair.setChuanganqiNumber(erpSaleRepairSale.getMaterielNumber());
            erpSaleRepairRepair.setMaterielName(erpSaleRepairSale.getMaterielName());
            erpSaleRepairRepair.setMaterielSpecifications(erpSaleRepairSale.getMaterielSpecifications());
            erpSaleRepairRepair.setMaterielModel(erpSaleRepairSale.getMaterielModel());
            erpSaleRepairRepair.setMaterielPlace(erpSaleRepairSale.getMaterielPlace());
            erpSaleRepairRepair.setMaterielState(erpSaleRepairSale.getMaterielState());
            erpSaleRepairRepair.setMaterielUnit(erpSaleRepairSale.getMaterielUnit());
            erpSaleRepairRepair.setMaterielProductionId(erpSaleRepairSale.getMaterielProductionId());
            erpSaleRepairRepair.setMaterielPressure(erpSaleRepairSale.getMaterielPressure());
            erpSaleRepairRepair.setMaterielNumber(erpSaleRepairSale.getMaterielNumber());
            erpSaleRepairRepair.setMaterielPrice(erpSaleRepairSale.getMaterielPrice());
            erpSaleRepairRepair.setMaterielTotal(erpSaleRepairSale.getMaterielTotal());
            erpSaleRepairRepair.setDynamicBalancingLeft(erpSaleRepairSale.getDynamicBalancingLeft());
            erpSaleRepairRepair.setDynamicBalancingRight(erpSaleRepairSale.getDynamicBalancingLeft());
            erpSaleRepairRepair.setCarPlate(erpSaleRepairSale.getCarPai());
            erpSaleRepairRepair.setShopShou(erpSaleRepairSale.getId());
            erpSaleRepairRepair.setState(1);


//                erpSaleRepairRepair.setMaterielName(erpSaleRepairSale.getMaterielName());
//                erpSaleRepairRepair.setMaterielName(erpSaleRepairSale.getMaterielName());
//                erpSaleRepairRepair.setMaterielName(erpSaleRepairSale.getMaterielName());
//            Double materielPressure =0.0;
//            try {
//                 materielPressure = erpSaleRepairSale.getMaterielPressure();
//
//
//            }catch (Exception e){
//                e.printStackTrace();
//            }


            if (erpSaleRepairSale.getDynamicBalancingLeft() != null || erpSaleRepairSale.getDynamicBalancingRight() != null && Double.valueOf(erpSaleRepairSale.getMaterielPressure()) != null) { //判断维修的项目 也就是维修什么

                erpSaleRepairRepair.setRepairRepair("拆装+动平衡");


            } else if (erpSaleRepairSale.getDynamicBalancingLeft() != null || erpSaleRepairSale.getDynamicBalancingRight() != null) {//判断维修的项目 也就是维修什么
                erpSaleRepairRepair.setRepairRepair("动平衡");
            } else if (erpSaleRepairSale.getMaterielPressure() != null) { //判断维修的项目 也就是维修什么
                erpSaleRepairRepair.setRepairRepair("拆装");
            }
            if (erpSaleRepairRepair.getRepairRepair() != null) { ////判断维修的项目 也就是维修什么 如果维修状态不为NULL

                if ((groupIds[0].equals("客户库存")) == false && (groupIds[0].equals("客户留存")) == false) { //并且去向是 去往库存的 那么 直接添加 erp_sale_repair_repair维修单里  直接去往对应仓库
                    erpSaleRepairRepairService.insert(erpSaleRepairRepair);

                }
                erpSaleRepairRepair.setShopShou(erpSaleRepairSale.getId());  //也就是说 销售单 对应维修单

            }

//            if (erpSaleRepairRepair!=null){
//
//                erpSaleRepairSale.setWuliaoweixiuTopId(erpSaleRepairRepair.getId());
//            }

//            ErpMateriel erpMateriel = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("warehouse_id", erpSaleRepairSale.getWarehouseId())
//                    .eq("materiel_name", erpSaleRepairSale.getMaterielName()).eq("materiel_brand", erpSaleRepairSale.getMaterielBrand()).eq("materiel_Specifications", erpSaleRepairSale.getMaterielSpecifications())
//                    .eq("materiel_place", erpSaleRepairSale.getMaterielPlace())
//                    .eq("materiel_state", erpSaleRepairSale.getMaterielState())
//                    .eq("materiel_unit", erpSaleRepairSale.getMaterielUnit())
//            );


            //物料流转用写错了之前写的是减去物料表的数量应该减去仓库表的数量
//            erpMateriel.setMaterielNumber(erpMateriel.getMaterielNumber()-erpSaleRepairSale.getMaterielNumber());
//            erpMaterielService.updateById(erpMateriel);
//            result.setData( erpSaleRepairSale.getId() );

            //定时器
//              Timer timer = new Timer();
            //timer.schedule(new MyTask(), 1000)                warehouse_id来源
            //更新车辆物料数据时候用的  津KYD178-左2外轮   materielOutTo  去向

//            String materielOutTo = erpSaleRepairSale.getMaterielOutTo();
            //查找你要处理的这个物料的在哪个仓库里面  找到对应仓库
            ErpMateriel erpCustomerWarehouse = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("materiel_production_id", erpSaleRepairRepair.getMaterielProductionId()));


            if (erpCustomerWarehouse != null) {
                //
//            erpCustomerWarehouse.setMaterielNumber(erpCustomerWarehouse.getMaterielNumber()-erpSaleRepairSale.getMaterielNumber());
                erpCustomerWarehouse.setDelectType(0);
                erpMaterielService.updateById(erpCustomerWarehouse);
                String[] groupId = erpSaleRepairSale.getMaterielOutTo().split("-");//把上面的去向截取
                if (groupId[0].equals("客户库存") || groupId[0].equals("客户留存")) {
                    ErpCustomerWarehouse war_name = erpCustomerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("war_name", groupId[1]));
                    ErpWarehouse warehouse_id = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", war_name).or().isNull("warehouse_id").
                            eq("materiel_name", erpSaleRepairSale.getMaterielName()).or().isNull("materiel_name").
                            eq("materiel_brand", erpSaleRepairSale.getMaterielBrand()).or().isNull("materiel_brand").
                            eq("materiel_Specifications", erpSaleRepairSale.getMaterielSpecifications()).or().isNull("materiel_Specifications")
                            .eq("materiel_place", erpSaleRepairSale.getMaterielPlace()).or().isNull("materiel_place")
                            .eq("materiel_state", erpSaleRepairSale.getMaterielState()).or().isNull("materiel_state")
                            .eq("materiel_unit", erpSaleRepairSale.getMaterielUnit()).or().isNull("materiel_unit"));

                    if (warehouse_id != null) {
                        warehouse_id.setMaterielNumber(warehouse_id.getMaterielNumber() + erpSaleRepairSale.getMaterielNumber());
                        erpWarehouseService.updateById(warehouse_id);
                    }


                } else {
                    ErpMateriel erpMateriel1 = null;
                    ErpCustomerCar cusCar = erpCustomerCarMapper.selectByPlate(erpSaleRepairRepair.getCarPlate());
                    ErpCarTireArchives car_id = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCar.getId()).eq("tire_location", groupId[1]));//.eq("car_id", cusCar.getId()
                    ////2222
                    ErpTireSensor tire_id = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("tire_id", car_id.getTireId()));///111
                    if (tire_id != null && erpSaleRepairSale.getMaterielName().contains("轮胎")) {
                        erpMateriel1 = erpMaterielService.selectById(tire_id.getTireId()); //这里的car_ids.getTireId() , 应该改成  tire_id里对应的通过判断

                    } else if (tire_id != null && erpSaleRepairSale.getMaterielName().contains("传感器")) {
                        erpMateriel1 = erpMaterielService.selectById(tire_id.getSensorId()); //这里的car_ids.getTireId() , 应该改成  tire_id里对应的通过判断

                    } else if (tire_id != null && erpSaleRepairSale.getMaterielName().contains("轮辋")) {
                        erpMateriel1 = erpMaterielService.selectById(tire_id.getRimsId()); //这里的car_ids.getTireId() , 应该改成  tire_id里对应的通过判断

                    }

//                erp_tire_archives_work
//                ErpTireArchivesWork tire_location = erpTireArchivesWorkService.selectOne(new EntityWrapper<ErpTireArchivesWork>().eq("tire_location", erpSaleRepairSale.getMaterielOutTo()));
//                ErpMateriel erpMateriel1 = erpMaterielService.selectById(car_id.getTireId());
//                ErpMateriel erpMateriel2 = erpMaterielService.selectById(car_id.getTireId());
                    //
//                ErpMateriel erpMateriel2 =new ErpMateriel();
//                erpMateriel2=erpMateriel1;
                    if (erpMateriel1.getId() != null) {
                        erpMateriel1.setMaterielBrand(erpSaleRepairSale.getMaterielBrand());
                        erpMateriel1.setMaterielModel(erpSaleRepairSale.getMaterielModel());
                        erpMateriel1.setMaterielName(erpSaleRepairSale.getMaterielName());
                        erpMateriel1.setMaterielSpecifications(erpSaleRepairSale.getMaterielSpecifications());
                        erpMateriel1.setMaterielModel(erpSaleRepairSale.getMaterielModel());
                        erpMateriel1.setMaterielPlace(erpSaleRepairSale.getMaterielPlace());
                        erpMateriel1.setMaterielState(erpSaleRepairSale.getMaterielState());
                        erpMateriel1.setMaterielUnit(erpSaleRepairSale.getMaterielUnit());
                        erpMateriel1.setMaterielProductionId(erpSaleRepairSale.getMaterielProductionId());
                        erpMateriel1.setMaterielNumber(erpSaleRepairSale.getMaterielNumber());
                        erpMateriel1.setDelectType(0);
                        erpMaterielService.updateById(erpMateriel1);
                    }
//                List<ErpSaleRepairRepair> l=new ArrayList<>();
//                List<ErpSaleRepairRepair> list=erpSaleRepairRepairMapper.selectList(new EntityWrapper<ErpSaleRepairRepair>().like("materiel_out_to", erpSaleRepairSale.getMaterielOutTo()).orderBy("id", false));
//                list.parallelStream().forEach( ls->{
//                    if (ls.getMaterielName().contains("轮胎")){
//                        l.add(ls);
//                    }
//                });
//


                    //   被定下来的车辆物料,走到待处理里
                    ErpSaleRepairRepair erpSaleRepairRepair2 = new ErpSaleRepairRepair();
                    erpSaleRepairRepair2.setMaterielNumber(1);
                    erpSaleRepairRepair2.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                    erpSaleRepairRepair2.setMaterielBrand(erpMateriel2.getMaterielBrand());
//                erpSaleRepairRepair2.setMaterielOutTo(erpSaleRepairSale.getMaterielOutTo());
                    erpSaleRepairRepair2.setMaterielModel(erpMateriel2.getMaterielModel());
                    erpSaleRepairRepair2.setMaterielName(erpMateriel2.getMaterielName());
                    erpSaleRepairRepair2.setMaterielSpecifications(erpMateriel2.getMaterielSpecifications());
                    erpSaleRepairRepair2.setMaterielModel(erpMateriel2.getMaterielModel());
                    erpSaleRepairRepair2.setMaterielPlace(erpMateriel2.getMaterielPlace());
                    erpSaleRepairRepair2.setMaterielState(erpMateriel2.getMaterielState());
                    erpSaleRepairRepair2.setMaterielUnit(erpMateriel2.getMaterielUnit());
                    erpSaleRepairRepair2.setMaterielProductionId(erpMateriel2.getMaterielProductionId());
                    erpSaleRepairRepair2.setMaterielPressure(erpSaleRepairSale.getMaterielPressure());
                    erpSaleRepairRepair2.setMaterielNumber(erpMateriel2.getMaterielNumber());
                    erpSaleRepairRepair2.setMaterielPrice(erpSaleRepairSale.getMaterielPrice());
                    erpSaleRepairRepair2.setMaterielTotal(erpSaleRepairSale.getMaterielTotal()); //去向应该是空的因为没有去向
                    erpSaleRepairRepair2.setDynamicBalancingLeft(erpSaleRepairSale.getDynamicBalancingLeft());
                    erpSaleRepairRepair2.setDynamicBalancingRight(erpSaleRepairSale.getDynamicBalancingLeft());
                    erpSaleRepairRepair2.setCarPlate(erpSaleRepairSale.getCarPai());
                    erpSaleRepairRepair2.setState(0);
                    erpSaleRepairRepair2.setShopShou(erpSaleRepairSale.getId());
                    erpSaleRepairRepairService.insert(erpSaleRepairRepair2);
                }


            }
            result.setMsg("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("添加失败");
        }
        return result;
    }


    @RequestMapping("/xiaoshouweixiuAddShangPinLieBiao_lxl")
    @ApiOperation("销售维修-添加商品列表_lxl")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carPai", value = "车牌号", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> xiaoshouweixiuAddShangPinLieBiao_lxl(ErpSaleRepairSale erpSaleRepairSale) {

        Result<Integer> result = new Result<Integer>();
        int customerid = erpSaleRepairSale.getId();
//        erpSaleRepairSale.setOrderId(BlankUtil.createID());
        erpSaleRepairSale.setOrderId("1");
        try {
            //津KYD178[0]      -左1轮  [1] 去向
            String[] groupIds = erpSaleRepairSale.getMaterielOutTo().split("-");//把上面的去向截取
            //销售维修轮胎维修单 也就是销售的轮胎的去向哪里维修什么东西
            ErpSaleRepairRepair erpSaleRepairRepair = new ErpSaleRepairRepair();
            //查找轮胎或者其他物料 所在轮胎的位置 在哪个轮胎上
            ErpTireSensor objj = null;

            //查找对应规格的要销售的物料
            List<ErpMateriel> listErpMateriel = erpMaterielService.selectList(new EntityWrapper<ErpMateriel>().eq("warehouse_id", erpSaleRepairSale.getWarehouseId()).
                    eq("materiel_name", erpSaleRepairSale.getMaterielName()).
                    eq("materiel_brand", erpSaleRepairSale.getMaterielBrand()).
                    eq("materiel_Specifications", erpSaleRepairSale.getMaterielSpecifications())
                    .eq("materiel_place", erpSaleRepairSale.getMaterielPlace())
                    .eq("materiel_state", erpSaleRepairSale.getMaterielState())
                    .eq("materiel_unit", erpSaleRepairSale.getMaterielUnit()).eq("delect_type", 1).eq("kucunorliucun", "库存"));

            ErpMateriel erpMateriel_Xswl = null;//销售的物料
            ErpWarehouse cangkuYuan = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", erpSaleRepairSale.getWarehouseId()).
                    eq("materiel_name", erpSaleRepairSale.getMaterielName()).
                    eq("materiel_brand", erpSaleRepairSale.getMaterielBrand()).
                    eq("materiel_Specifications", erpSaleRepairSale.getMaterielSpecifications())
                    .eq("materiel_place", erpSaleRepairSale.getMaterielPlace())
                    .eq("materiel_state", erpSaleRepairSale.getMaterielState())
                    .eq("materiel_unit", erpSaleRepairSale.getMaterielUnit()).eq("customer_id", customerid));
            boolean pan = false;

            for (ErpMateriel obj : listErpMateriel) {//查找对应销售的物料
                if (erpSaleRepairSale.getMaterielProductionId().trim().equals("")) {
                    if (obj.getMaterielProductionId().trim().equals(erpSaleRepairSale.getMaterielProductionId())) {
                        erpMateriel_Xswl = obj;
                        break;
                    } else {
                        erpMateriel_Xswl = obj;

                    }
                } else {
                    if (obj.getMaterielProductionId().trim().equals(erpSaleRepairSale.getMaterielProductionId())) {
                        erpMateriel_Xswl = obj;
                        break;
                    } else {
                        erpMateriel_Xswl = obj;
                        erpMateriel_Xswl.setMaterielProductionId(erpSaleRepairSale.getMaterielProductionId());

                    }
                }

            }


            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            erpMateriel_Xswl.setMaterielTaiyaid(erpSaleRepairSale.getMaterielTaiyaid() + "");
            ErpMateriel erpMateriel2 = new ErpMateriel();//查找将被替换的物料 类型： 轮胎or传感器or轮辋
            //判断是去向是 车辆还是 库存
            if(groupIds[0].equals("报废丢弃")){
                erpMateriel_Xswl.setState(4);
                ErpWarehouse cangkuYuanchuanganqi = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", erpMateriel_Xswl.getWarehouseId()).
                        eq("materiel_name", erpMateriel_Xswl.getMaterielName()).
                        eq("materiel_brand", erpMateriel_Xswl.getMaterielBrand()).
                        eq("materiel_Specifications", erpMateriel_Xswl.getMaterielSpecifications())
                        .eq("materiel_place", erpMateriel_Xswl.getMaterielPlace())
                        .eq("materiel_state", erpMateriel_Xswl.getMaterielState())
                        .eq("materiel_unit", erpMateriel_Xswl.getMaterielUnit()).eq("customer_id", customerid));
                if (cangkuYuanchuanganqi.getMaterielNumber() - 1 > 0) {
                    cangkuYuanchuanganqi.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                }

                cangkuYuanchuanganqi.setAllPrice(cangkuYuanchuanganqi.getAllPrice() - erpMateriel_Xswl.getMaterielStoragePrice());
                erpWarehouseService.updateById(cangkuYuanchuanganqi);
            } else if ((groupIds[0].equals("客户库存")) == false && (groupIds[0].equals("客户留存")) == false) {  //判断是去向车上  判断对应车上的将要顶替的物料
                //添加BI数据  的操作 只有往备胎上面放数据的时候 访问BI逻辑
                if (groupIds.length>1&&groupIds[1].contains("备胎")) {
                    //查询对应车辆
                    List<ErpCustomerCar> erpCustomerCarList = erpCustomerCarMapper.selectList(new EntityWrapper<ErpCustomerCar>().eq("car_plate", erpSaleRepairSale.getCarPai()));
                    List<ErpSystemParametersChild> parameters1 = erpSystemParametersChildService.selectList(new EntityWrapper<ErpSystemParametersChild>().eq("pid", 54));//"车载BI"
                    for (ErpSystemParametersChild obj : parameters1) {
                        if (obj.getName().trim().equals(erpMateriel_Xswl.getMaterielName())) {
                            ErpCustomerCar car = erpCustomerCarList.get(0);
                            List<ErpUser> us = erpUserService.selectList(new EntityWrapper<ErpUser>().eq("customer_id", customerid));
                            insertBI(erpMateriel_Xswl.getMaterielName(), car.getCarType1(), car.getCarNumber(), car.getCarPlate(), customerid, car.getCarWheel(), us.get(0).getId(), us.get(0).getRealName(), erpMateriel_Xswl.getMaterielProductionId());
                        }
                    }

                }


                //判断去向的哪辆车
                ErpCustomerCar cusCars = erpCustomerCarMapper.selectByPlate(groupIds[0]);
                //查询去向车上对应的 替换调位置的物料（如左一伦是什么物料）
                ErpCarTireArchives car_ids = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCars.getId()).eq("tire_location", groupIds[1]));//.eq("car_id", cusCar.getId()
                if (car_ids == null) {
                    car_ids = new ErpCarTireArchives();
                    car_ids.setCarId(cusCars.getId());
                    car_ids.setTireLocation(groupIds[1]);
                    car_ids.setTireId(0);
                    erpCarTireArchivesService.insert(car_ids);
                    car_ids = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCars.getId()).eq("tire_location", groupIds[1]));//.eq("car_id", cusCar.getId()
                }
                //查找对应的轮胎是否配置记录 也就是轮胎对应轮辋 对应传感器
                ErpTireSensor ers = new ErpTireSensor();
                //查询对应的替换调的轮胎上的轮胎和轮胎上的传感器
                objj = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("erpcartirearchivesid", car_ids.getId()));
                if (objj == null) {
                    objj = new ErpTireSensor();
                    objj.setBt("1");
                    objj.setErpcartirearchivesid(car_ids.getId());
                    erpTireSensorService.insert(objj);
                    objj = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("erpcartirearchivesid", car_ids.getId()));
                }
                //如果原物料是轮胎  并且替换的车的物料该位置也是轮胎 那么替换这个车的该位置的轮胎的所有物料
//                if(objj==null){
//                    ers.setTireId(erpMateriel_Xswl.getId());
//                    ers.setTirePrice(erpSaleRepairSale.getMaterielPrice());
//                    ers.setBt("1");
//                }
                if (erpMateriel_Xswl.getZhiNeng()) {

                    //查询替换掉的智能轮胎id物料 不是智能轮胎传感器id
                    erpMateriel2 = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", car_ids.getTireId()));
                    //判断替换掉的是否是智能轮胎
                    if (erpMateriel2 != null && erpMateriel2.getZhiNeng()) {
                        ErpTireSensor sennew = new ErpTireSensor();
                        sennew = objj;
                        sennew.setErpcartirearchivesid(0);
                        sennew.setRimsPrice(0.0);
                        sennew.setRimsId(0);
                        erpTireSensorService.insert(sennew);

                        //查询智能轮胎上的传感器物料
                        ErpMateriel erpMaterielchuangganqi = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", erpMateriel2.getCorrespondingId()));
                        //替换掉原车上智能轮胎带的传感器
                        objj.setSensorId(erpMateriel_Xswl.getCorrespondingId());
                        objj.setSensorPrice(erpMaterielchuangganqi.getMaterielStoragePrice());
                        car_ids.setSensorsId(erpMateriel_Xswl.getCorrespondingId());
                        //查询被顶替的轮胎的维修物料表
                        erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", erpMateriel2.getId()));
                        if (erpMateriel2 != null) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMateriel2.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairSale.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMateriel2.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMateriel2.getMaterielModel());

                            erpSaleRepairRepair_daichuli.setMaterielName(erpMateriel2.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMateriel2.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMateriel2.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMateriel2.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMateriel2.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMateriel2.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMateriel2.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);

                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMateriel2.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairSale.getCarPai());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMateriel2.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMateriel2.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }
                    } else if (erpMateriel2 != null) {
                        //替换掉的轮胎 的上面的零件
                        ErpMateriel thdlj = null;
                        //查询轮胎对应的销售维修物料 设置成待处理
                        if (erpMateriel2 != null) {
                            //erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", erpMateriel2.getId()));
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(erpMateriel2.getId());

                        }


                        //查找对应轮胎上的传感器 是否有物料 设置成待处理
                        if (objj != null && objj.getSensorId1() != null && objj.getSensorId1() != 0) {
                            erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getSensorId1()));
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId1());
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairSale.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairSale.getCarPai());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }

                            objj.setSensorId1(0);
                        }


                        //查找对应轮胎上 是否有轮胎物料 设置成待处理
                        if (objj != null && objj.getTireId() != null && objj.getTireId() != 0) {
                            erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getTireId()));
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getTireId());
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairSale.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairSale.getCarPai());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }

                        }

                        car_ids.setTireId(erpMateriel_Xswl.getId());
                        car_ids.setTirePurchasePrice(erpMateriel_Xswl.getMaterielStoragePrice());
                        car_ids.setTirePurchaseDate(df.format(new Date()));
                        if (objj != null) {
                            objj.setTireId(erpMateriel_Xswl.getId());
                            objj.setTirePrice(erpMateriel_Xswl.getMaterielStoragePrice());
                            erpTireSensorService.updateById(objj);
                        }
                        erpCarTireArchivesService.updateById(car_ids);
                    }

                    if (objj != null) {
                        objj.setTireId(erpMateriel_Xswl.getId());
                        objj.setTirePrice(erpMateriel_Xswl.getMaterielStoragePrice());
                        objj.setSensorId(erpMateriel_Xswl.getCorrespondingId());
                        erpTireSensorService.updateById(objj);
                    }
                    car_ids.setTireId(erpMateriel_Xswl.getId());

                    car_ids.setTirePurchasePrice(erpMateriel_Xswl.getMaterielStoragePrice());
                    car_ids.setTirePurchaseDate(df.format(new Date()));
                    erpCarTireArchivesService.updateById(car_ids);
                } else if (erpMateriel_Xswl.getMaterielName().contains("轮胎")) {

                    //查询替换掉的轮胎物料
                    erpMateriel2 = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", car_ids.getTireId()));
                    //判断如果这辆车上的轮胎位置没有物料那么不替换


//                    //替换掉的轮胎
//                        if(objj==null){
//                            ers.setTireId(erpMateriel_Xswl.getId());
//                            ers.setTirePrice(erpSaleRepairSale.getMaterielPrice());
//                            ers.setBt("1");
//                        }
                    //替换掉的轮胎 的上面的零件
                    ErpMateriel thdlj = null;
                    //查询轮胎对应的销售维修物料 设置成待处理
                    if (erpMateriel2 != null) {
                        //erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", erpMateriel2.getId()));
                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(erpMateriel2.getId());
                        if (erpMaterieldai != null) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairSale.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);

                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairSale.getCarPai());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }
                    }


                    //查找对应轮胎上的传感器 是否有物料 并且判断车上是否是只能轮胎 如果是把传感器 设置成待处理
                    if (objj != null && objj.getSensorId() != null && objj.getSensorId() != 0) {

                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId());
                        if (erpMaterieldai != null && !erpMateriel2.getZhiNeng()) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairSale.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairSale.getCarPai());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }

                        objj.setSensorId(0);
                    }


//                    //查找对应轮胎上 是否有轮胎物料 设置成待处理
//                    if (objj != null && objj.getTireId() != null && objj.getTireId() != 0) {
//                        erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getTireId()));
//                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getTireId());
//                        if (erpMaterieldai != null) {
//                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
//                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
//                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairSale.getMaterielOutTo());
//                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
//                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
//                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
//                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
//                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
//                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
//                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
//                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
//                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
//                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
//                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
//                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
//                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);
//                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
//                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
//                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
//                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairSale.getCarPai());
//                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
//                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
//                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
//                            erpSaleRepairRepair_daichuli.setState(0);
//                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
//                        }
//
//                    }

                    car_ids.setTireId(erpMateriel_Xswl.getId());
                    car_ids.setTirePurchasePrice(erpMateriel_Xswl.getMaterielStoragePrice());
                    car_ids.setTirePurchaseDate(df.format(new Date()));
                    if (objj != null) {
                        objj.setTireId(erpMateriel_Xswl.getId());
                        objj.setTirePrice(erpMateriel_Xswl.getMaterielStoragePrice());
                        erpTireSensorService.updateById(objj);
                    }
                    erpCarTireArchivesService.updateById(car_ids);


                } else if (erpMateriel_Xswl.getMaterielName().contains("传感器")) {

                    //查询替换调的是传感器 并且判断传感器是放在轮胎上 还是轮辋上
                    if (erpMateriel_Xswl.getMaterielSpecifications().trim().equals("粘贴式")) { //放在轮胎上

                        ErpMateriel erpMaterielluntai = erpMaterielService.selectById(objj.getTireId());
                        //判断原有被顶物料是否是普通轮胎  如果是那么和上的粘贴式传感器组合成智能轮胎
                        if (erpMaterielluntai != null && !erpMaterielluntai.getZhiNeng()) {
                            erpMaterielluntai.setCorrespondingId(erpMateriel_Xswl.getId());
                            erpMaterielluntai.setTai(true);
                            erpMaterielluntai.setZhiNeng(true);
                            erpMaterielluntai.setMaterielTaiyaid(erpMateriel_Xswl.getMaterielProductionId());
                            erpMaterielService.updateById(erpMaterielluntai);
                            erpMateriel_Xswl.setCorrespondingId(erpMaterielluntai.getId());
                            erpMateriel_Xswl.setMaterielEqcode(erpMaterielluntai.getMaterielEqcode());
                            erpMateriel_Xswl.setMaterielRfid(erpMaterielluntai.getMaterielRfid());
                            erpMaterielService.updateById(erpMateriel_Xswl);
                        }
                        if (objj.getSensorId() != null && !objj.getSensorId().equals("")) { //判断车上的传感器 不是空  那么就替换调

                            //查询被顶的智能轮胎传感器的物料销售单
                            erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getSensorId()));
                            //被定下来的轮胎传感器物料
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId());
                            //判断车上的物料智能轮胎和传感器是否是一个组合 如果是一个组合拆分掉重组
                            if (erpMaterieldai != null && erpMaterieldai.getCorrespondingId().equals(objj.getTireId())) {
                                //查找对应的智能轮胎的轮胎物料
                                ErpMateriel erpMaterielLuntai = erpMaterielService.selectById(objj.getTireId());
                                erpMaterielLuntai.setCorrespondingId(erpMateriel_Xswl.getId());
                                erpMaterielLuntai.setMaterielTaiyaid(erpMateriel_Xswl.getMaterielProductionId());
                                erpMaterielService.updateById(erpMaterielLuntai);
                                erpMateriel_Xswl.setCorrespondingId(objj.getTireId());
                                erpMaterielService.updateById(erpMateriel_Xswl);
                                erpMaterieldai.setCorrespondingId(0);
                                erpMaterielService.updateById(erpMaterieldai);
                            }
                            //如果该传感器没有销售单 那么证明他是智能轮胎的物料带进来的 在else要添加一个被定下来的粘贴时传感器的销售单
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairSale.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairSale.getCarPai());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }

                        }
                        objj.setSensorId(erpMateriel_Xswl.getId());
                        objj.setSensorPrice(erpMateriel_Xswl.getMaterielStoragePrice());
                        erpTireSensorService.updateById(objj);
                        car_ids.setSensorsId(erpMateriel_Xswl.getId());
                        erpCarTireArchivesService.updateById(car_ids);
                    } else { //放在轮辋上的传感器

//                        objj = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("tire_id", car_ids.getTireId()).eq("bt", "1"));
                        //判断车上的传感器 不是空  那么就替换调

                        if (objj.getSensorId1() != null && !objj.getSensorId1().equals("")) {
                            erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getSensorId1()));
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId1());
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairSale.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairSale.getCarPai());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }
                        }
                        car_ids.setSensorsId(erpMateriel_Xswl.getId());
                        erpCarTireArchivesService.updateById(car_ids);
                        objj.setSensorId1(erpMateriel_Xswl.getId());
                        objj.setSensorPrice(erpMateriel_Xswl.getMaterielStoragePrice());
                        erpTireSensorService.updateById(objj);
//                        erpTireSensorService.updateById(objj);
                    }

                } else if (erpMateriel_Xswl.getMaterielName().contains("轮辋")) {
                    //查询替换调的是轮辋id 那么查询该车上的位置的轮胎是否有轮辋id

                    //查找对应轮辋上的传感器 是否有物料 设置成待处理
                    if (objj != null && objj.getSensorId1() != null && objj.getSensorId1() != 0) {
                        erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getSensorId1()));
                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId1());
                        if (erpMaterieldai != null) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairSale.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairSale.getCarPai());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }
                        objj.setSensorId1(0);
                    }
                    //查找对应轮胎上 是否有轮辋物料 设置成待处理
                    if (objj != null && objj.getRimsId() != null && !objj.getRimsId().equals("")) {
                        erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getRimsId()));
                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId1());
                        if (erpMaterieldai != null) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairSale.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairSale.getCarPai());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }

                    }
                    objj.setRimsId(erpMateriel_Xswl.getId());
                    objj.setRimsPrice(erpMateriel_Xswl.getMaterielStoragePrice());
                    erpTireSensorService.updateById(objj);

                }
                if (erpMateriel_Xswl.getMaterielName().contains("轮胎(智能)")) {
                    ErpMateriel erpMaterielChuanganqi = erpMaterielService.selectById(erpMateriel_Xswl.getCorrespondingId());
                    ErpWarehouse cangkuYuanchuanganqi = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", erpMaterielChuanganqi.getWarehouseId()).
                            eq("materiel_name", erpMaterielChuanganqi.getMaterielName()).
                            eq("materiel_brand", erpMaterielChuanganqi.getMaterielBrand()).
                            eq("materiel_Specifications", erpMaterielChuanganqi.getMaterielSpecifications())
                            .eq("materiel_place", erpMaterielChuanganqi.getMaterielPlace())
                            .eq("materiel_state", erpMaterielChuanganqi.getMaterielState())
                            .eq("materiel_unit", erpMaterielChuanganqi.getMaterielUnit()).eq("customer_id", customerid));
                    erpMaterielChuanganqi.setWarehouseId(erpMateriel_Xswl.getWarehouseId());
                    erpMaterielChuanganqi.setDelectType(1);
                    erpMaterielService.updateById(erpMaterielChuanganqi);
                    //操作的原仓库物料 减一个物料
                    if (cangkuYuanchuanganqi.getMaterielNumber() - 1 > 0) {
                        cangkuYuanchuanganqi.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                    }

                    cangkuYuanchuanganqi.setAllPrice(cangkuYuanchuanganqi.getAllPrice() - erpMaterielChuanganqi.getMaterielStoragePrice());
                    erpWarehouseService.updateById(cangkuYuanchuanganqi);


                }
                erpMateriel_Xswl.setDelectType(0);
                erpMateriel_Xswl.setMaterielLoad(1);
                erpMaterielService.updateById(erpMateriel_Xswl);

                cangkuYuan.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                cangkuYuan.setAllPrice(cangkuYuan.getAllPrice() - erpMateriel_Xswl.getMaterielStoragePrice());
                erpWarehouseService.updateById(cangkuYuan);
            } else {//去向不是车的操作


                //操作如果去向是仓库的 将要改变去向和原仓库的个数
                //查找对应的去向仓库
                ErpCustomerWarehouse war_name = erpCustomerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("war_name", groupIds[1]));
                //查找对应的去向仓库物料
                ErpWarehouse warehouse_id = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", war_name.getId()).
                        eq("materiel_name", erpSaleRepairSale.getMaterielName()).
                        eq("materiel_brand", erpSaleRepairSale.getMaterielBrand()).
                        eq("materiel_Specifications", erpSaleRepairSale.getMaterielSpecifications())
                        .eq("materiel_place", erpSaleRepairSale.getMaterielPlace())
                        .eq("materiel_state", erpSaleRepairSale.getMaterielState())
                        .eq("materiel_unit", erpSaleRepairSale.getMaterielUnit()).eq("customer_id", customerid));


                if ((groupIds[0].equals("客户库存")) == true) {
                    //去向仓库需要加一个 物料
                    if (warehouse_id != null) {
                        warehouse_id.setMaterielNumber(warehouse_id.getMaterielNumber() + erpSaleRepairSale.getMaterielNumber());
                        warehouse_id.setAllPrice(warehouse_id.getAllPrice() + erpMateriel_Xswl.getMaterielStoragePrice());
                        erpWarehouseService.updateById(warehouse_id);
                    } else {
                        //判断如果去向的仓库没有这个物料那么加一个仓库物料
                        warehouse_id = new ErpWarehouse();
                        warehouse_id.setMaterielNumber(1);
                        warehouse_id.setCustomerId(customerid);
                        warehouse_id.setMaterielName(erpSaleRepairSale.getMaterielName());
                        warehouse_id.setMaterielBrand(erpSaleRepairSale.getMaterielBrand());
                        warehouse_id.setMaterielSpecifications(erpSaleRepairSale.getMaterielSpecifications());
                        warehouse_id.setMaterielPlace(erpSaleRepairSale.getMaterielPlace());
                        warehouse_id.setMaterielState(erpSaleRepairSale.getMaterielState());
                        warehouse_id.setMaterielUnit(erpSaleRepairSale.getMaterielUnit());
                        warehouse_id.setWarehouseId(war_name.getId());
                        warehouse_id.setMaterielProductionId(erpMateriel_Xswl.getMaterielProductionId());
                        warehouse_id.setMaterielTax(erpMateriel_Xswl.getMaterielStoragePrice());
                        warehouse_id.setMaterielNotax(erpMateriel_Xswl.getMaterielStoragePrice());
                        warehouse_id.setAllPrice(erpMateriel_Xswl.getMaterielStoragePrice());
                        warehouse_id.setMaterielModel(erpMateriel_Xswl.getMaterielModel());
                        erpWarehouseService.insert(warehouse_id);
                    }
                    //操作原物料 和 原仓库数据
                    if (erpMateriel_Xswl != null) {
                        if (erpMateriel_Xswl.getMaterielName().contains("轮胎(智能)")) {
                            ErpMateriel erpMaterielChuanganqi = erpMaterielService.selectById(erpMateriel_Xswl.getCorrespondingId());
                            ErpWarehouse cangkuYuanchuanganqi = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", erpMaterielChuanganqi.getWarehouseId()).
                                    eq("materiel_name", erpMaterielChuanganqi.getMaterielName()).
                                    eq("materiel_brand", erpMaterielChuanganqi.getMaterielBrand()).
                                    eq("materiel_Specifications", erpMaterielChuanganqi.getMaterielSpecifications())
                                    .eq("materiel_place", erpMaterielChuanganqi.getMaterielPlace())
                                    .eq("materiel_state", erpMaterielChuanganqi.getMaterielState())
                                    .eq("materiel_unit", erpMaterielChuanganqi.getMaterielUnit()).eq("customer_id", customerid));
                            erpMaterielChuanganqi.setWarehouseId(war_name.getId());
                            erpMaterielChuanganqi.setDelectType(1);
                            erpMaterielService.updateById(erpMaterielChuanganqi);
                            //操作的原仓库物料 减一个物料
                            if (cangkuYuan.getMaterielNumber() - 1 > 0) {
                                cangkuYuanchuanganqi.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                            }
                            cangkuYuanchuanganqi.setAllPrice(cangkuYuanchuanganqi.getAllPrice() - erpMaterielChuanganqi.getMaterielStoragePrice());
                            erpWarehouseService.updateById(cangkuYuanchuanganqi);


                        }
                        //操作的物料 原物料减一个
                        erpMateriel_Xswl.setWarehouseId(war_name.getId());
                        erpMateriel_Xswl.setDelectType(1);
                        erpMateriel_Xswl.setMaterielLoad(0);
                        erpMaterielService.updateById(erpMateriel_Xswl);
                        //操作的原仓库物料 减一个物料
                        if (cangkuYuan.getMaterielNumber() - 1 > 0) {
                            cangkuYuan.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                        }
                        cangkuYuan.setAllPrice(cangkuYuan.getAllPrice() - erpMateriel_Xswl.getMaterielStoragePrice());
                        erpWarehouseService.updateById(cangkuYuan);
                    }
                } else {
                    if (erpMateriel_Xswl != null) {
                        if (erpMateriel_Xswl.getMaterielName().contains("轮胎(智能)")) {

                            ErpMateriel erpMaterielChuanganqi = erpMaterielService.selectById(erpMateriel_Xswl.getCorrespondingId());
                            //传感器的仓库物料
                            ErpWarehouse cangkuYuanchuanganqi = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", erpMaterielChuanganqi.getWarehouseId()).
                                    eq("materiel_name", erpMaterielChuanganqi.getMaterielName()).
                                    eq("materiel_brand", erpMaterielChuanganqi.getMaterielBrand()).
                                    eq("materiel_Specifications", erpMaterielChuanganqi.getMaterielSpecifications())
                                    .eq("materiel_place", erpMaterielChuanganqi.getMaterielPlace())
                                    .eq("materiel_state", erpMaterielChuanganqi.getMaterielState())
                                    .eq("materiel_unit", erpMaterielChuanganqi.getMaterielUnit()).eq("customer_id", customerid));
                            erpMaterielChuanganqi.setWarehouseId(war_name.getId());
                            erpMaterielChuanganqi.setKucunorliucun("留存");
                            erpMaterielChuanganqi.setDelectType(1);
                            //客户留存的客户id
                            erpMaterielChuanganqi.setCustomerId(customerid);
                            erpMaterielService.updateById(erpMaterielChuanganqi);
                            //操作的原仓库物料 减一个物料
                            if (cangkuYuanchuanganqi.getMaterielNumber() - 1 > 0) {
                                cangkuYuanchuanganqi.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                            }

                            cangkuYuanchuanganqi.setAllPrice(cangkuYuanchuanganqi.getAllPrice() - erpMaterielChuanganqi.getMaterielStoragePrice());
                            erpWarehouseService.updateById(cangkuYuanchuanganqi);
                        }
                        //操作的物料 原物料减一个
                        erpMateriel_Xswl.setWarehouseId(war_name.getId());
                        erpMateriel_Xswl.setKucunorliucun("留存");
                        erpMateriel_Xswl.setDelectType(1);
                        erpMateriel_Xswl.setMaterielLoad(0);
                        //客户留存的客户id
                        erpMateriel_Xswl.setCustomerId(customerid);
                        erpMaterielService.updateById(erpMateriel_Xswl);
                        //操作的原仓库物料 减一个物料
                        cangkuYuan.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                        cangkuYuan.setAllPrice(cangkuYuan.getAllPrice() - erpMateriel_Xswl.getMaterielStoragePrice());
                        erpWarehouseService.updateById(cangkuYuan);
                    }
                }
            }

            if (objj != null) {
                List<ErpTireSensor> ErpTireSensorList = erpTireSensorService.selectList(new EntityWrapper<ErpTireSensor>().eq("tire_id", objj.getTireId()));
                int maxid = 0;
                if (ErpTireSensorList.size() > 1) {
                    for (ErpTireSensor erpTireSensor : ErpTireSensorList) {
                        if (erpTireSensor.getErpcartirearchivesid() == null) {
                            erpTireSensorService.deleteById(erpTireSensor.getId());
                        }
                    }
                }
            }
            //添加销售单
            ErpSaleRepairSale esrs = new ErpSaleRepairSale();
            erpSaleRepairSale.setId(esrs.getId());
            erpSaleRepairSale.setErpMaterielId(erpMateriel_Xswl.getId());
            erpSaleRepairSale.setCustomerId(customerid);
            erpSaleRepairSaleService.insert(erpSaleRepairSale);


            erpSaleRepairRepair = new ErpSaleRepairRepair();
            //添加 销售维修-轮胎维修表
            erpSaleRepairRepair.setErpMaterielId(erpMateriel_Xswl.getId());
            erpSaleRepairRepair.setMaterielSource(erpCustomerWarehouseService.selectById(erpSaleRepairSale.getWarehouseId()).getWarName());
            erpSaleRepairRepair.setMaterielBrand(erpSaleRepairSale.getMaterielBrand());
            erpSaleRepairRepair.setMaterielOutTo(erpSaleRepairSale.getMaterielOutTo());
            erpSaleRepairRepair.setMaterielModel(erpSaleRepairSale.getMaterielModel());
            erpSaleRepairRepair.setMaterielName(erpSaleRepairSale.getMaterielName());
            erpSaleRepairRepair.setMaterielSpecifications(erpSaleRepairSale.getMaterielSpecifications());
            erpSaleRepairRepair.setMaterielModel(erpSaleRepairSale.getMaterielModel());
            erpSaleRepairRepair.setMaterielPlace(erpSaleRepairSale.getMaterielPlace());
            erpSaleRepairRepair.setMaterielState(erpSaleRepairSale.getMaterielState());
            erpSaleRepairRepair.setMaterielUnit(erpSaleRepairSale.getMaterielUnit());
            erpSaleRepairRepair.setMaterielProductionId(erpSaleRepairSale.getMaterielProductionId());
            erpSaleRepairRepair.setMaterielPressure(erpSaleRepairSale.getMaterielPressure());
            erpSaleRepairRepair.setMaterielNumber(erpSaleRepairSale.getMaterielNumber());
            erpSaleRepairRepair.setMaterieId(erpMateriel_Xswl.getId());
            erpSaleRepairRepair.setMaterielTotal(erpSaleRepairSale.getMaterielTotal());
            erpSaleRepairRepair.setDynamicBalancingLeft(erpSaleRepairSale.getDynamicBalancingLeft());
            erpSaleRepairRepair.setDynamicBalancingRight(erpSaleRepairSale.getDynamicBalancingLeft());
            erpSaleRepairRepair.setCarPlate(erpSaleRepairSale.getCarPai());
            erpSaleRepairRepair.setShopShou(erpSaleRepairSale.getId());
            erpSaleRepairRepair.setMaterielTaiyaid(erpSaleRepairSale.getMaterielTaiyaid());
            erpSaleRepairRepair.setState(1);
            erpSaleRepairRepair.setCustomerId(customerid);

            if ((erpSaleRepairSale.getDynamicBalancingLeft() != null || erpSaleRepairSale.getDynamicBalancingRight() != null) && erpSaleRepairSale.getMaterielPressure() != null) {
                erpSaleRepairRepair.setRepairRepair("轮胎拆装,动 平 衡");
            } else if (erpSaleRepairSale.getDynamicBalancingLeft() != null || erpSaleRepairSale.getDynamicBalancingRight() != null) {
                erpSaleRepairRepair.setRepairRepair("动 平 衡");
            } else if (erpSaleRepairSale.getMaterielPressure() != null) {
                erpSaleRepairRepair.setRepairRepair("轮胎拆装");
            } else {
                erpSaleRepairRepair.setRepairRepair("");
            }
            double price = 0.0;
            String[] a = erpSaleRepairRepair.getRepairRepair().split(",");
            if (a.length >= 2) {
                for (int i = 0; i < a.length; i++) {
                    ErpContractRepair erpContractRepair = repairService.selectOne(new EntityWrapper<ErpContractRepair>().eq("pid", customerid).eq("rep_name", a[i]));
                    if (null != erpContractRepair) {


                        if (erpContractRepair.getRepPrice() != null) {
                            price += Double.valueOf(erpContractRepair.getRepPrice());
                        }
                    }
                }
            }
            erpSaleRepairRepair.setMaterielPrice(price);
            erpSaleRepairRepair.setMaterielTotal(price);
            if (!erpSaleRepairRepair.getRepairRepair().equals("")) {
                erpSaleRepairRepairService.insert(erpSaleRepairRepair);
            }


            result.setMsg("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("添加失败");
        }


        return result;
    }


    @RequestMapping("/addHc")
    @ApiOperation("添加物料耗材")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "price", value = "价格", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> addHc(ErpHc erpHc) {
        Result<Integer> result = new Result<Integer>();
        try {

            erpHcService.insert(erpHc);
            result.setData(erpHc.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/selectHcByCarPlate")
    @ApiOperation("查询物料耗材")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "price", value = "价格", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpHc>> selectHcByCarPlate(String carPlate) {
        Result<List<ErpHc>> result = new Result<>();
        try {

            List<ErpHc> car_plate = erpHcService.selectList(new EntityWrapper<ErpHc>().eq("car_plate", carPlate));
            result.setData(car_plate);
            result.setCount(car_plate.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/selectHcByCarPlateByOrder")
    @ApiOperation("查询物料耗材")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "price", value = "价格", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpHc>> selectHcByCarPlateByOrder(String order) {
        Result<List<ErpHc>> result = new Result<>();
        try {

            List<ErpHc> car_plate = erpHcService.selectList(new EntityWrapper<ErpHc>().eq("order_id", order));
            result.setData(car_plate);
            result.setCount(car_plate.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/addCheLiangWeiXiuTop")
    @ApiOperation("添加车辆维修上面的")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "price", value = "价格", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> addCheLiangWeiXiuTop(ErpSaleRepairGarage erpHc) {
        Result<Integer> result = new Result<Integer>();
        try {
            ErpInsurance insurancepolicyno = erpInsuranceService.selectOne(new EntityWrapper<ErpInsurance>().eq("insurancepolicyno", erpHc.getInsurancepolicyno()));
            if (null != insurancepolicyno) {
                erpInsuranceService.updateById(insurancepolicyno);
            } else {
                ErpInsurance erpInsurance = new ErpInsurance();
                if (null != erpHc.getInsurancepolicyno() && null != erpHc.getInsuranceCompany()) {

                    erpInsurance.setCarNumber(erpHc.getCarPai());
                    erpInsurance.setInsurancepolicyno(erpHc.getInsurancepolicyno());
                    erpInsurance.setInsuranceCompany(erpHc.getInsuranceCompany());
                    erpInsuranceService.insertOrUpdate(erpInsurance);
                }
            }
            String replace = erpHc.getCarPai().replace(" ", "");
            erpHc.setCarPai(replace);
            erpSaleRepairGarageService.insert(erpHc);
            result.setData(erpHc.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/addCheLiangWeiXiuDown")
    @ApiOperation("添加车辆维修下面的")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "price", value = "价格", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> addCheLiangWeiXiuDown(ErpSaleRepairSupplies erpHc) {
        Result<Integer> result = new Result<Integer>();
        try {

            erpSaleRepairSuppliesService.insert(erpHc);
            result.setData(erpHc.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/addWuLiaoWeiXiuNew_lxl")
    @ApiOperation("物料维修")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "price", value = "价格", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Map<String, Object>> addWuLiaoWeiXiuNew_lxl(ErpSaleRepairRepair erpSaleRepairRepair, String json) {
        //来源
        String[] groups = erpSaleRepairRepair.getMaterielSource().split("-");
        //去向
        String[] group = erpSaleRepairRepair.getMaterielOutTo().split("-");

        int customerid = erpSaleRepairRepair.getState();
        // 添加维修项目
        ErpSaleRepairRepair erpSaleRepairRepair2 = new ErpSaleRepairRepair();
        Result<Map<String, Object>> result = new Result<>();
        Map<String, Object> map = new HashMap<>();

        //查找轮胎或者其他物料 所在轮胎的位置 在哪个轮胎上
        ErpTireSensor objj = null;
        //查询要维修的物料
        ErpMateriel erpMateriel = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", erpSaleRepairRepair.getId()));
        //查询要维修的物料的仓库
        ErpWarehouse cangkuYuan =  erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", erpMateriel.getWarehouseId()).
                eq("materiel_name", erpMateriel.getMaterielName()).
                eq("materiel_brand", erpMateriel.getMaterielBrand()).
                eq("materiel_Specifications", erpMateriel.getMaterielSpecifications())
                .eq("materiel_place", erpMateriel.getMaterielPlace())
                .eq("materiel_state", erpMateriel.getMaterielState())
                .eq("materiel_unit", erpMateriel.getMaterielUnit()).eq("customer_id", customerid));
        //查找对应的维修单
        int fj=0;
        if(erpSaleRepairRepair.getFj()!=null){
            fj=erpSaleRepairRepair.getFj();
        }
        ErpSaleRepairRepair esrr = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("id", fj));

        try {
            //消耗的物料
            List<ErpSaleRepairSupplies> erpSaleRepairSupplies = JSONArray.parseArray(json, ErpSaleRepairSupplies.class);
            //获取到哪个去向仓库
            ErpCustomerWarehouse war_name = erpCustomerWarehouseService.selectOne(new EntityWrapper<ErpCustomerWarehouse>().eq("war_name", group[1]));
            //处理维修后去到哪里

            //查找对应规格的要销售的物料
            List<ErpMateriel> listErpMateriel = erpMaterielService.selectList(new EntityWrapper<ErpMateriel>().eq("warehouse_id", erpMateriel.getWarehouseId()).
                    eq("materiel_name", erpSaleRepairRepair.getMaterielName()).
                    eq("materiel_brand", erpSaleRepairRepair.getMaterielBrand()).
                    eq("materiel_Specifications", erpSaleRepairRepair.getMaterielSpecifications())
                    .eq("materiel_place", erpSaleRepairRepair.getMaterielPlace())
                    .eq("materiel_state", erpSaleRepairRepair.getMaterielState())
                    .eq("materiel_unit", erpSaleRepairRepair.getMaterielUnit()).eq("delect_type", 1).eq("kucunorliucun", "库存"));

            for (ErpMateriel obj : listErpMateriel) {//查找对应销售的物料
                if (erpSaleRepairRepair.getMaterielProductionId().trim().equals("")) {
                    if (obj.getMaterielProductionId().trim().equals(erpSaleRepairRepair.getMaterielProductionId())) {
                        erpMateriel = obj;
                        break;
                    } else {
                        erpMateriel = obj;

                    }
                } else {
                    if (obj.getMaterielProductionId().trim().equals(erpSaleRepairRepair.getMaterielProductionId())) {
                        erpMateriel = obj;
                        break;
                    } else {
                        erpMateriel = obj;
                        erpMateriel.setMaterielProductionId(erpSaleRepairRepair.getMaterielProductionId());

                    }
                }

            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            erpMateriel.setMaterielTaiyaid(erpSaleRepairRepair.getMaterielTaiyaid() + "");
            ErpMateriel erpMateriel2 = new ErpMateriel();//查找将被替换的物料 类型： 轮胎or传感器or轮辋
            //判断去向是报废丢弃的操作
            if(group[0].equals("报废丢弃")){
                erpMateriel.setState(4);
                if(!groups[0].contains("存")) {
                    //判断来源的哪辆车
                    ErpCustomerCar cusCars_laiyuan = erpCustomerCarMapper.selectByPlate(groups[0]);

                    //查询对应的来源车
                    ErpCarTireArchives car_ids_laiyuan = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCars_laiyuan.getId()).eq("tire_location", groups[1]));//.eq("car_id", cusCar.getId()

                    //处理原车量
                    ErpMateriel erpMaterielTire = erpMaterielService.selectById(car_ids_laiyuan.getTireId());
                    //查询对应的替换调的轮胎上的轮胎和轮胎上的传感器
                    ErpTireSensor objj_laiyuan = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("erpcartirearchivesid", car_ids_laiyuan.getId()));


                    if (esrr == null && erpMaterielTire != null && erpMaterielTire.getZhiNeng() && erpMateriel.getMaterielName().contains("轮胎") && erpMateriel.getZhiNeng()) {
                        car_ids_laiyuan.setSensorsId(0);
                        car_ids_laiyuan.setTirePurchasePrice(0.0);

                        car_ids_laiyuan.setTireId(0);
                        objj_laiyuan.setSensorPrice(0.0);
                        objj_laiyuan.setSensorId(0);
                        objj_laiyuan.setTirePrice(0.0);
                        objj_laiyuan.setTireId(0);
                    } else if (erpMaterielTire != null && !erpMaterielTire.getZhiNeng() && erpMateriel.getMaterielName().contains("轮胎")) {
                        objj_laiyuan.setTirePrice(0.0);
                        objj_laiyuan.setTireId(0);
                        car_ids_laiyuan.setTirePurchasePrice(0.0);
                        car_ids_laiyuan.setTireId(0);
                    } else if (erpMateriel.getMaterielName().contains("传感器")) {
                        objj_laiyuan.setSensorPrice(0.0);
                        objj_laiyuan.setSensorId(0);
                        objj_laiyuan.setSensorPrice(0.0);
                        objj_laiyuan.setSensorId(0);
                    } else if (erpMateriel.getMaterielName().contains("轮辋")) {
                        car_ids_laiyuan.setRimId(0);
                        objj_laiyuan.setSensorId1(0);
                        objj_laiyuan.setRimsId(0);
                        objj_laiyuan.setRimsPrice(0.0);
                    }
                    erpCarTireArchivesService.updateById(car_ids_laiyuan);
                    erpTireSensorService.updateById(objj_laiyuan);
                }else{
                    ErpWarehouse cangkuYuanchuanganqi = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", erpMateriel.getWarehouseId()).
                            eq("materiel_name", erpMateriel.getMaterielName()).
                            eq("materiel_brand", erpMateriel.getMaterielBrand()).
                            eq("materiel_Specifications", erpMateriel.getMaterielSpecifications())
                            .eq("materiel_place", erpMateriel.getMaterielPlace())
                            .eq("materiel_state", erpMateriel.getMaterielState())
                            .eq("materiel_unit", erpMateriel.getMaterielUnit()).eq("customer_id", customerid));
                    if (cangkuYuanchuanganqi.getMaterielNumber() - 1 > 0) {
                        cangkuYuanchuanganqi.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                    }

                    cangkuYuanchuanganqi.setAllPrice(cangkuYuanchuanganqi.getAllPrice() - erpMateriel.getMaterielStoragePrice());
                    erpWarehouseService.updateById(cangkuYuanchuanganqi);
                }
                erpMaterielService.updateById(erpMateriel);
            } else if (groups[0].contains("存") && !group[0].contains("存")) {  //判断是去向车上  判断对应车上的将要顶替的物料
                //添加BI数据  的操作 只有往备胎上面放数据的时候 访问BI逻辑
                if (group[1].contains("备胎")) {
                    //查询对应车辆
                    List<ErpCustomerCar> erpCustomerCarList = erpCustomerCarMapper.selectList(new EntityWrapper<ErpCustomerCar>().eq("car_plate", erpSaleRepairRepair.getCarPlate()));
                    List<ErpSystemParametersChild> parameters1 = erpSystemParametersChildService.selectList(new EntityWrapper<ErpSystemParametersChild>().eq("pid", 54));//"车载BI"
                    for (ErpSystemParametersChild obj : parameters1) {
                        if (obj.getName().trim().equals(erpMateriel.getMaterielName())) {
                            ErpCustomerCar car = erpCustomerCarList.get(0);
                            List<ErpUser> us = erpUserService.selectList(new EntityWrapper<ErpUser>().eq("customer_id", customerid));
                            insertBI(erpMateriel.getMaterielName(), car.getCarType1(), car.getCarNumber(), car.getCarPlate(), customerid, car.getCarWheel(), us.get(0).getId(), us.get(0).getRealName(), erpMateriel.getMaterielProductionId());
                        }
                    }
                }


                //判断去向的哪辆车
                ErpCustomerCar cusCars = erpCustomerCarMapper.selectByPlate(group[0]);
                //查询去向车上对应的 替换调位置的物料（如左一伦是什么物料）
                ErpCarTireArchives car_ids = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCars.getId()).eq("tire_location", group[1]));//.eq("car_id", cusCar.getId()
                if (car_ids == null) {
                    car_ids = new ErpCarTireArchives();
                    car_ids.setCarId(cusCars.getId());
                    car_ids.setTireLocation(group[1]);
                    car_ids.setTireId(0);
                    erpCarTireArchivesService.insert(car_ids);
                    car_ids = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCars.getId()).eq("tire_location", group[1]));//.eq("car_id", cusCar.getId()
                }
                //查找对应的轮胎是否配置记录 也就是轮胎对应轮辋 对应传感器
                ErpTireSensor ers = new ErpTireSensor();
                //查询对应的替换调的轮胎上的轮胎和轮胎上的传感器
                objj = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("erpcartirearchivesid", car_ids.getId()));
                if (objj == null) {
                    objj = new ErpTireSensor();
                    objj.setBt("1");
                    objj.setErpcartirearchivesid(car_ids.getId());
                    erpTireSensorService.insert(objj);
                    objj = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("erpcartirearchivesid", car_ids.getId()));
                }
                //如果原物料是轮胎  并且替换的车的物料该位置也是轮胎 那么替换这个车的该位置的轮胎的所有物料

                if (erpMateriel.getZhiNeng()) {

                    //查询替换掉的智能轮胎id物料 不是智能轮胎传感器id
                    erpMateriel2 = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", car_ids.getTireId()));
                    //判断替换掉的是否是智能轮胎
                    if (erpMateriel2 != null && erpMateriel2.getZhiNeng()) {
                        ErpTireSensor sennew = new ErpTireSensor();
                        sennew = objj;
                        sennew.setErpcartirearchivesid(0);
                        sennew.setRimsPrice(0.0);
                        sennew.setRimsId(0);
                        erpTireSensorService.insert(sennew);

                        //查询智能轮胎上的传感器物料
                        ErpMateriel erpMaterielchuangganqi = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", erpMateriel2.getCorrespondingId()));
                        //替换掉原车上智能轮胎带的传感器
                        objj.setSensorId(erpMateriel.getCorrespondingId());
                        objj.setSensorPrice(erpMaterielchuangganqi.getMaterielStoragePrice());
                        car_ids.setSensorsId(erpMateriel.getCorrespondingId());
                        //查询被顶替的轮胎的维修物料表
                       // erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", erpMateriel2.getId()));
                        if (erpMateriel2 != null) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMateriel2.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMateriel2.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMateriel2.getMaterielModel());

                            erpSaleRepairRepair_daichuli.setMaterielName(erpMateriel2.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMateriel2.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMateriel2.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMateriel2.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMateriel2.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMateriel2.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMateriel2.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);

                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMateriel2.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMateriel2.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMateriel2.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }
                    } else if (erpMateriel2 != null) {
                        //替换掉的轮胎 的上面的零件
                        ErpMateriel thdlj = null;
                        //查询轮胎对应的销售维修物料 设置成待处理
                        if (erpMateriel2 != null) {
                            //erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", erpMateriel2.getId()));
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(erpMateriel2.getId());

                        }


                        //查找对应轮胎上的传感器 是否有物料 设置成待处理
                        if (objj != null && objj.getSensorId1() != null && objj.getSensorId1() != 0) {
                           // erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getSensorId1()));
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId1());
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }

                            objj.setSensorId1(0);
                        }


                        //查找对应轮胎上 是否有轮胎物料 设置成待处理
                        if (objj != null && objj.getTireId() != null && objj.getTireId() != 0) {
                           // erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getTireId()));
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getTireId());
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }

                        }

                        car_ids.setTireId(erpMateriel.getId());
                        car_ids.setTirePurchasePrice(erpMateriel.getMaterielStoragePrice());
                        car_ids.setTirePurchaseDate(df.format(new Date()));
                        if (objj != null) {
                            objj.setTireId(erpMateriel.getId());
                            objj.setTirePrice(erpMateriel.getMaterielStoragePrice());
                            erpTireSensorService.updateById(objj);
                        }
                        erpCarTireArchivesService.updateById(car_ids);
                    }
                    ErpMateriel erpMaterieldaigetCorrespondingId = erpMaterielService.selectById(erpMateriel.getCorrespondingId());
                    if (objj != null) {

                        objj.setTireId(erpMateriel.getId());
                        objj.setTirePrice(erpMateriel.getMaterielStoragePrice());
                        objj.setSensorId(erpMateriel.getCorrespondingId());

                        objj.setSensorPrice(erpMaterieldaigetCorrespondingId.getMaterielStoragePrice());
                        erpTireSensorService.updateById(objj);
                    }
                    car_ids.setTireId(erpMateriel.getId());
                    car_ids.setSensorsPurchasePrice(erpMaterieldaigetCorrespondingId.getMaterielStoragePrice());
                    car_ids.setTirePurchasePrice(erpMateriel.getMaterielStoragePrice());
                    car_ids.setTirePurchaseDate(df.format(new Date()));
                    erpCarTireArchivesService.updateById(car_ids);
                } else if (erpMateriel.getMaterielName().contains("轮胎")) {

                    //查询替换掉的轮胎物料
                    erpMateriel2 = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", car_ids.getTireId()));
                    //判断如果这辆车上的轮胎位置没有物料那么不替换


                    //替换掉的轮胎 的上面的零件
                    ErpMateriel thdlj = null;
                    //查询轮胎对应的销售维修物料 设置成待处理
                    if (erpMateriel2 != null) {
                        //erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", erpMateriel2.getId()));
                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(erpMateriel2.getId());
                        if (erpMaterieldai != null) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);

                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }
                    }


                    //查找对应轮胎上的传感器 是否有物料 并且判断车上是否是只能轮胎 如果是把传感器 设置成待处理
                    if (objj != null && objj.getSensorId() != null && objj.getSensorId() != 0) {

                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId());
                        if (erpMaterieldai != null && !erpMateriel2.getZhiNeng()) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }

                        objj.setSensorId(0);
                    }


                    car_ids.setTireId(erpMateriel.getId());
                    car_ids.setTirePurchasePrice(erpMateriel.getMaterielStoragePrice());
                    car_ids.setTirePurchaseDate(df.format(new Date()));
                    if (objj != null) {
                        objj.setTireId(erpMateriel.getId());
                        objj.setTirePrice(erpMateriel.getMaterielStoragePrice());
                        erpTireSensorService.updateById(objj);
                    }
                    erpCarTireArchivesService.updateById(car_ids);


                } else if (erpMateriel.getMaterielName().contains("传感器")) {

                    //查询替换调的是传感器 并且判断传感器是放在轮胎上 还是轮辋上
                    if (erpMateriel.getMaterielSpecifications().trim().equals("粘贴式")) { //放在轮胎上

                        ErpMateriel erpMaterielluntai = erpMaterielService.selectById(objj.getTireId());
                        //判断原有被顶物料是否是普通轮胎  如果是那么和上的粘贴式传感器组合成智能轮胎
                        if (erpMaterielluntai != null && !erpMaterielluntai.getZhiNeng()) {
                            erpMaterielluntai.setCorrespondingId(erpMateriel.getId());
                            erpMaterielluntai.setTai(true);
                            erpMaterielluntai.setZhiNeng(true);
                            erpMaterielluntai.setMaterielTaiyaid(erpMateriel.getMaterielProductionId());
                            erpMaterielService.updateById(erpMaterielluntai);
                            erpMateriel.setCorrespondingId(erpMaterielluntai.getId());
                            erpMateriel.setMaterielEqcode(erpMaterielluntai.getMaterielEqcode());
                            erpMateriel.setMaterielRfid(erpMaterielluntai.getMaterielRfid());
                            erpMaterielService.updateById(erpMateriel);
                        }
                        if (objj.getSensorId() != null && !objj.getSensorId().equals("")) { //判断车上的传感器 不是空  那么就替换调

                            //查询被顶的智能轮胎传感器的物料销售单

                            //被定下来的轮胎传感器物料
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId());
                            //判断车上的物料智能轮胎和传感器是否是一个组合 如果是一个组合拆分掉重组
                            if (erpMaterieldai != null && erpMaterieldai.getCorrespondingId().equals(objj.getTireId())) {
                                //查找对应的智能轮胎的轮胎物料
                                ErpMateriel erpMaterielLuntai = erpMaterielService.selectById(objj.getTireId());
                                erpMaterielLuntai.setCorrespondingId(erpMateriel.getId());
                                erpMaterielLuntai.setMaterielTaiyaid(erpMateriel.getMaterielProductionId());
                                erpMaterielService.updateById(erpMaterielLuntai);
                                erpMateriel.setCorrespondingId(objj.getTireId());
                                erpMaterielService.updateById(erpMateriel);
                                erpMaterieldai.setCorrespondingId(0);
                                erpMaterielService.updateById(erpMaterieldai);
                            }
                            //如果该传感器没有销售单 那么证明他是智能轮胎的物料带进来的 在else要添加一个被定下来的粘贴时传感器的销售单
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }

                        }
                        objj.setSensorId(erpMateriel.getId());
                        objj.setSensorPrice(erpMateriel.getMaterielStoragePrice());
                        erpTireSensorService.updateById(objj);
                        car_ids.setSensorsId(erpMateriel.getId());
                        erpCarTireArchivesService.updateById(car_ids);
                    } else { //放在轮辋上的传感器

//                        objj = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("tire_id", car_ids.getTireId()).eq("bt", "1"));
                        //判断车上的传感器 不是空  那么就替换调

                        if (objj.getSensorId1() != null && !objj.getSensorId1().equals("")) {
                            erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getSensorId1()));
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId1());
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }
                        }
                        car_ids.setSensorsId(erpMateriel.getId());
                        erpCarTireArchivesService.updateById(car_ids);
                        objj.setSensorId1(erpMateriel.getId());
                        objj.setSensorPrice(erpMateriel.getMaterielStoragePrice());
                        erpTireSensorService.updateById(objj);
//                        erpTireSensorService.updateById(objj);
                    }

                } else if (erpMateriel.getMaterielName().contains("轮辋")) {
                    //查询替换调的是轮辋id 那么查询该车上的位置的轮胎是否有轮辋id

                    //查找对应轮辋上的传感器 是否有物料 设置成待处理
                    if (objj != null && objj.getSensorId1() != null && objj.getSensorId1() != 0) {
                        erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getSensorId1()));
                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId1());
                        if (erpMaterieldai != null) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }
                        objj.setSensorId1(0);
                    }
                    //查找对应轮胎上 是否有轮辋物料 设置成待处理
                    if (objj != null && objj.getRimsId() != null && !objj.getRimsId().equals("")) {
                        erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getRimsId()));
                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId1());
                        if (erpMaterieldai != null) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }

                    }
                    objj.setRimsId(erpMateriel.getId());
                    objj.setRimsPrice(erpMateriel.getMaterielStoragePrice());
                    erpTireSensorService.updateById(objj);

                }
                if (erpMateriel.getMaterielName().contains("轮胎(智能)")) {
                    ErpMateriel erpMaterielChuanganqi = erpMaterielService.selectById(erpMateriel.getCorrespondingId());
                    ErpWarehouse cangkuYuanchuanganqi = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", erpMaterielChuanganqi.getWarehouseId()).
                            eq("materiel_name", erpMaterielChuanganqi.getMaterielName()).
                            eq("materiel_brand", erpMaterielChuanganqi.getMaterielBrand()).
                            eq("materiel_Specifications", erpMaterielChuanganqi.getMaterielSpecifications())
                            .eq("materiel_place", erpMaterielChuanganqi.getMaterielPlace())
                            .eq("materiel_state", erpMaterielChuanganqi.getMaterielState())
                            .eq("materiel_unit", erpMaterielChuanganqi.getMaterielUnit()).eq("customer_id", customerid));
                    erpMaterielChuanganqi.setWarehouseId(erpMateriel.getWarehouseId());
                    erpMaterielChuanganqi.setDelectType(0);
                    erpMaterielChuanganqi.setMaterielLoad(1);
                    erpMaterielService.updateById(erpMaterielChuanganqi);
                    //操作的原仓库物料 减一个物料
                    if (cangkuYuanchuanganqi.getMaterielNumber() - 1 > 0) {
                        cangkuYuanchuanganqi.setMaterielNumber(cangkuYuanchuanganqi.getMaterielNumber() - 1);
                    }

                    cangkuYuanchuanganqi.setAllPrice(cangkuYuanchuanganqi.getAllPrice() - erpMaterielChuanganqi.getMaterielStoragePrice());
                    erpWarehouseService.updateById(cangkuYuanchuanganqi);


                }

                if (cangkuYuan.getMaterielNumber() > 0 && erpMateriel.getDelectType() == 1) {
                    erpMateriel.setDelectType(0);
                    erpMateriel.setMaterielLoad(1);
                    erpMaterielService.updateById(erpMateriel);
                    cangkuYuan.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                    cangkuYuan.setAllPrice(cangkuYuan.getAllPrice() - erpMateriel.getMaterielStoragePrice());
                }
                erpWarehouseService.updateById(cangkuYuan);

            } else if (!groups[0].contains("存") && !group[0].contains("存")) {


                //判断去向的哪辆车
                ErpCustomerCar cusCars = erpCustomerCarMapper.selectByPlate(group[0]);

                //查询去向车上对应的 替换调位置的物料（如左一伦是什么物料）
                ErpCarTireArchives car_ids = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCars.getId()).eq("tire_location", group[1]));//.eq("car_id", cusCar.getId()
                if (car_ids == null) {
                    car_ids = new ErpCarTireArchives();
                    car_ids.setCarId(cusCars.getId());
                    car_ids.setTireLocation(group[1]);
                    car_ids.setTireId(0);
                    erpCarTireArchivesService.insert(car_ids);
                    car_ids = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCars.getId()).eq("tire_location", group[1]));//.eq("car_id", cusCar.getId()
                }
                //查找对应的轮胎是否配置记录 也就是轮胎对应轮辋 对应传感器
                ErpTireSensor ers = new ErpTireSensor();
                //查询对应的替换调的轮胎上的轮胎和轮胎上的传感器
                objj = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("erpcartirearchivesid", car_ids.getId()));
                if (objj == null) {
                    objj = new ErpTireSensor();
                    objj.setBt("1");
                    objj.setErpcartirearchivesid(car_ids.getId());
                    erpTireSensorService.insert(objj);
                    objj = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("erpcartirearchivesid", car_ids.getId()));
                }

                //判断来源的哪辆车
                ErpCustomerCar cusCars_laiyuan = erpCustomerCarMapper.selectByPlate(groups[0]);

                //查询对应的来源车
                ErpCarTireArchives car_ids_laiyuan = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCars_laiyuan.getId()).eq("tire_location", groups[1]));//.eq("car_id", cusCar.getId()
                if (car_ids_laiyuan == null) {
                    car_ids_laiyuan = new ErpCarTireArchives();
                    car_ids_laiyuan.setCarId(cusCars.getId());
                    car_ids_laiyuan.setTireLocation(group[1]);
                    car_ids_laiyuan.setTireId(0);
                    erpCarTireArchivesService.insert(car_ids_laiyuan);
                    car_ids_laiyuan = erpCarTireArchivesService.selectOne(new EntityWrapper<ErpCarTireArchives>().eq("car_id", cusCars_laiyuan.getId()).eq("tire_location", groups[1]));//.eq("car_id", cusCar.getId()
                }

                //查询对应的替换调的轮胎上的轮胎和轮胎上的传感器
                ErpTireSensor  objj_laiyuan = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("erpcartirearchivesid", car_ids_laiyuan.getId()));
                if (objj_laiyuan == null) {
                    objj_laiyuan = new ErpTireSensor();
                    objj_laiyuan.setBt("1");
                    objj_laiyuan.setErpcartirearchivesid(car_ids.getId());
                    erpTireSensorService.insert(objj_laiyuan);
                    objj_laiyuan = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("erpcartirearchivesid", car_ids_laiyuan.getId()));
                }


                //如果原物料是轮胎  并且替换的车的物料该位置也是轮胎 那么替换这个车的该位置的轮胎的所有物料

                if (erpMateriel.getZhiNeng()) {

                    //查询替换掉的智能轮胎id物料 不是智能轮胎传感器id
                    erpMateriel2 = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", car_ids.getTireId()));
                    //判断替换掉的是否是智能轮胎
                    if (erpMateriel2 != null && erpMateriel2.getZhiNeng()) {

                        //查询替换掉的智能轮胎上的传感器物料
                        ErpMateriel erpMaterielchuangganqi = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", erpMateriel2.getCorrespondingId()));

                        //替换掉原车上智能轮胎带的传感器
                        objj.setSensorId(erpMateriel.getCorrespondingId());
                        objj.setSensorPrice(erpMaterielchuangganqi.getMaterielStoragePrice());
                        car_ids.setSensorsId(erpMateriel.getCorrespondingId());
                        //查询被顶替的轮胎的维修物料表

                        //查找对应轮胎上 是否有轮胎物料 设置成待处理
                        if (objj != null && objj.getTireId() != null && objj.getTireId() != 0) {
//                            erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getTireId()));
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getTireId());
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }

                        }
//                        }
                    } else if (erpMateriel2 != null) {//判断顶替的物料如果是空 并且是被顶的物料是普通的轮胎

                        //查找对应智能轮胎上的传感器 是否有物料 设置成待处理
                        if (objj != null && objj.getSensorId() != null && objj.getSensorId() != 0) {

                            //查询智能轮胎上的传感器
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId());
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }

                            objj.setSensorId(erpMaterieldai.getId());
                        }


                        //查找对应轮胎上 是否有轮胎物料 设置成待处理
                        if (objj != null && objj.getTireId() != null && objj.getTireId() != 0) {
//                            erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", objj.getTireId()));
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getTireId());
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }

                        }


                    }

                    if (objj != null) {
                        objj.setTireId(erpMateriel.getId());
                        objj.setTirePrice(erpMateriel.getMaterielStoragePrice());
                        objj.setSensorId(erpMateriel.getCorrespondingId());
                        erpTireSensorService.updateById(objj);
                    }
                    car_ids.setTireId(erpMateriel.getId());

                    car_ids.setTirePurchasePrice(erpMateriel.getMaterielStoragePrice());
                    car_ids.setTirePurchaseDate(df.format(new Date()));
                    erpCarTireArchivesService.updateById(car_ids);
                } else if (erpMateriel.getMaterielName().contains("轮胎")&&!erpMateriel.getZhiNeng()) {

                    //查询替换掉的轮胎物料
                    erpMateriel2 = erpMaterielService.selectOne(new EntityWrapper<ErpMateriel>().eq("id", car_ids.getTireId()));
                    //判断如果这辆车上的轮胎位置没有物料那么不替换

                    //替换掉的轮胎 的上面的零件
                    ErpMateriel thdlj = null;
                    //查询轮胎对应的销售维修物料 设置成待处理
                    if (erpMateriel2 != null) {
                        //erpSaleRepairRepair = erpSaleRepairRepairService.selectOne(new EntityWrapper<ErpSaleRepairRepair>().eq("erpMaterielId", erpMateriel2.getId()));
                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(erpMateriel2.getId());
                        if (erpMaterieldai != null) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);

                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }
                    }


                    //查找对应轮胎上的传感器 是否有物料 并且判断车上是否是只能轮胎 如果是把传感器 设置成待处理
                    if (objj != null && objj.getSensorId() != null && objj.getSensorId() != 0) {

                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId());
                        if (erpMaterieldai != null && !erpMateriel2.getZhiNeng()) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }

                        objj.setSensorId(0);
                    }


                    car_ids.setTireId(erpMateriel.getId());
                    car_ids.setTirePurchasePrice(erpMateriel.getMaterielStoragePrice());
                    car_ids.setTirePurchaseDate(df.format(new Date()));
                    if (objj != null) {
                        objj.setTireId(erpMateriel.getId());
                        objj.setTirePrice(erpMateriel.getMaterielStoragePrice());
                        erpTireSensorService.updateById(objj);
                    }
                    erpCarTireArchivesService.updateById(car_ids);


                } else if (erpMateriel.getMaterielName().contains("传感器")) {

                    //查询替换调的是传感器 并且判断传感器是放在轮胎上 还是轮辋上
                    if (erpMateriel.getMaterielSpecifications().trim().equals("粘贴式")) { //放在轮胎上

                        ErpMateriel erpMaterielluntai = erpMaterielService.selectById(objj.getTireId());
                        //判断原有被顶物料是否是普通轮胎  如果是那么和上的粘贴式传感器组合成智能轮胎
                        if (erpMaterielluntai != null && !erpMaterielluntai.getZhiNeng()) {
                            erpMaterielluntai.setCorrespondingId(erpMateriel.getId());
                            erpMaterielluntai.setTai(true);
                            erpMaterielluntai.setZhiNeng(true);
                            erpMaterielluntai.setMaterielTaiyaid(erpMateriel.getMaterielProductionId());
                            erpMaterielService.updateById(erpMaterielluntai);
                            erpMateriel.setCorrespondingId(erpMaterielluntai.getId());
                            erpMateriel.setMaterielEqcode(erpMaterielluntai.getMaterielEqcode());
                            erpMateriel.setMaterielRfid(erpMaterielluntai.getMaterielRfid());
                            erpMaterielService.updateById(erpMateriel);
                        }
                        if (objj.getSensorId() != null && !objj.getSensorId().equals("")) { //判断车上的传感器 不是空  那么就替换调


                            //被定下来的轮胎传感器物料
                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId());
                            //判断车上的物料智能轮胎和传感器是否是一个组合 如果是一个组合拆分掉重组
                            if (erpMaterieldai != null && erpMaterieldai.getCorrespondingId().equals(objj.getTireId())) {
                                //查找对应的智能轮胎的轮胎物料
                                ErpMateriel erpMaterielLuntai = erpMaterielService.selectById(objj.getTireId());
                                erpMaterielLuntai.setCorrespondingId(erpMateriel.getId());
                                erpMaterielLuntai.setMaterielTaiyaid(erpMateriel.getMaterielProductionId());
                                erpMaterielService.updateById(erpMaterielLuntai);
                                erpMateriel.setCorrespondingId(objj.getTireId());
                                erpMaterielService.updateById(erpMateriel);
                                erpMaterieldai.setCorrespondingId(0);
                                erpMaterielService.updateById(erpMaterieldai);
                            }
                            //如果该传感器没有销售单 那么证明他是智能轮胎的物料带进来的 在else要添加一个被定下来的粘贴时传感器的销售单
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }

                        }
                        objj.setSensorId(erpMateriel.getId());
                        objj.setSensorPrice(erpMateriel.getMaterielStoragePrice());
                        erpTireSensorService.updateById(objj);
                        car_ids.setSensorsId(erpMateriel.getId());
                        erpCarTireArchivesService.updateById(car_ids);
                    } else { //放在轮辋上的传感器

//                        objj = erpTireSensorService.selectOne(new EntityWrapper<ErpTireSensor>().eq("tire_id", car_ids.getTireId()).eq("bt", "1"));
                        //判断车上的传感器 不是空  那么就替换调

                        if (objj.getSensorId1() != null && !objj.getSensorId1().equals("")) {

                            ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId1());
                            if (erpMaterieldai != null) {
                                ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                                erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                                erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                                erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                                erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                                erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                                erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                                erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                                erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                                erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                                erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                                erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                                erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                                erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                                erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                                erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                                erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                                erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                                erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                                erpSaleRepairRepair_daichuli.setState(0);
                                erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                            }
                        }
                        car_ids.setSensorsId(erpMateriel.getId());
                        erpCarTireArchivesService.updateById(car_ids);
                        objj.setSensorId1(erpMateriel.getId());
                        objj.setSensorPrice(erpMateriel.getMaterielStoragePrice());
                        erpTireSensorService.updateById(objj);
//                        erpTireSensorService.updateById(objj);
                    }

                } else if (erpMateriel.getMaterielName().contains("轮辋")) {
                    //查询替换调的是轮辋id 那么查询该车上的位置的轮胎是否有轮辋id

                    //查找对应轮辋上的传感器 是否有物料 设置成待处理
                    if (objj != null && objj.getSensorId1() != null && objj.getSensorId1() != 0) {
                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId1());
                        if (erpMaterieldai != null) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }
                        objj.setSensorId1(0);
                    }
                    //查找对应轮胎上 是否有轮辋物料 设置成待处理
                    if (objj != null && objj.getRimsId() != null && !objj.getRimsId().equals("")) {

                        ErpMateriel erpMaterieldai = erpMaterielService.selectById(objj.getSensorId1());
                        if (erpMaterieldai != null) {
                            ErpSaleRepairRepair erpSaleRepairRepair_daichuli = new ErpSaleRepairRepair();
                            erpSaleRepairRepair_daichuli.setErpMaterielId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielSource(erpSaleRepairRepair.getMaterielOutTo());
                            erpSaleRepairRepair_daichuli.setMaterielBrand(erpMaterieldai.getMaterielBrand());
                            erpSaleRepairRepair_daichuli.setMaterielOutTo("");
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielName(erpMaterieldai.getMaterielName());
                            erpSaleRepairRepair_daichuli.setMaterielSpecifications(erpMaterieldai.getMaterielSpecifications());
                            erpSaleRepairRepair_daichuli.setMaterielModel(erpMaterieldai.getMaterielModel());
                            erpSaleRepairRepair_daichuli.setMaterielPlace(erpMaterieldai.getMaterielPlace());
                            erpSaleRepairRepair_daichuli.setMaterielState(erpMaterieldai.getMaterielState());
                            erpSaleRepairRepair_daichuli.setMaterielUnit(erpMaterieldai.getMaterielUnit());
                            erpSaleRepairRepair_daichuli.setMaterielProductionId(erpMaterieldai.getMaterielProductionId());
                            erpSaleRepairRepair_daichuli.setMaterielPressure(0.0);
                            erpSaleRepairRepair_daichuli.setMaterielNumber(1);
                            erpSaleRepairRepair_daichuli.setCustomerId(customerid);
                            erpSaleRepairRepair_daichuli.setMaterielTotal(erpMaterieldai.getMaterielStoragePrice());
                            erpSaleRepairRepair_daichuli.setDynamicBalancingLeft(0);
                            erpSaleRepairRepair_daichuli.setDynamicBalancingRight(0);
                            erpSaleRepairRepair_daichuli.setCarPlate(erpSaleRepairRepair.getCarPlate());
                            erpSaleRepairRepair_daichuli.setShopShou(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterieId(erpMaterieldai.getId());
                            erpSaleRepairRepair_daichuli.setMaterielTaiyaid("");
                            erpSaleRepairRepair_daichuli.setState(0);
                            erpSaleRepairRepairService.insert(erpSaleRepairRepair_daichuli);
                        }

                    }
                    objj.setRimsId(erpMateriel.getId());
                    objj.setRimsPrice(erpMateriel.getMaterielStoragePrice());
                    erpTireSensorService.updateById(objj);

                }


                //处理原车量
                ErpMateriel erpMaterielTire=erpMaterielService.selectById(car_ids_laiyuan.getTireId());
                if (esrr == null&&erpMaterielTire!=null&&erpMaterielTire.getZhiNeng()&&erpMateriel.getMaterielName().contains("轮胎")&&erpMateriel.getZhiNeng()) {
                    car_ids_laiyuan.setSensorsId(0);
                    car_ids_laiyuan.setTirePurchasePrice(0.0);
                    car_ids_laiyuan.setTireId(0);
                    objj_laiyuan.setSensorPrice(0.0);
                    objj_laiyuan.setSensorId(0);
                    objj_laiyuan.setTirePrice(0.0);
                    objj_laiyuan.setTireId(0);
                }else if(erpMaterielTire!=null&&!erpMaterielTire.getZhiNeng()&&erpMateriel.getMaterielName().contains("轮胎")){
                    objj_laiyuan.setTirePrice(0.0);
                    objj_laiyuan.setTireId(0);
                    car_ids_laiyuan.setTirePurchasePrice(0.0);
                    car_ids_laiyuan.setTireId(0);
                }else if(erpMateriel.getMaterielName().contains("传感器")){
                    objj_laiyuan.setSensorPrice(0.0);
                    objj_laiyuan.setSensorId(0);
                    objj_laiyuan.setSensorPrice(0.0);
                    objj_laiyuan.setSensorId(0);
                }else if (erpMateriel.getMaterielName().contains("轮辋")) {
                    car_ids_laiyuan.setRimId(0);
                    objj_laiyuan.setSensorId1(0);
                    objj_laiyuan.setRimsId(0);
                    objj_laiyuan.setRimsPrice(0.0);
                }
                erpCarTireArchivesService.updateById(car_ids_laiyuan);
                erpTireSensorService.updateById(objj_laiyuan);

            } else {  //判断是去向车上  判断对应车上的将要顶替的物料

                //查找对应的去向仓库物料
                ErpWarehouse warehouse_id = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", war_name.getId()).
                        eq("materiel_name", erpMateriel.getMaterielName()).
                        eq("materiel_brand", erpMateriel.getMaterielBrand()).
                        eq("materiel_Specifications", erpMateriel.getMaterielSpecifications())
                        .eq("materiel_place", erpMateriel.getMaterielPlace())
                        .eq("materiel_state", erpMateriel.getMaterielState())
                        .eq("materiel_unit", erpMateriel.getMaterielUnit()).eq("customer_id", erpSaleRepairRepair.getState()));

                if ((group[0].equals("客户库存")) == true) {
                    //去向仓库需要加一个 物料
                    if (warehouse_id != null) {
                        warehouse_id.setMaterielNumber(warehouse_id.getMaterielNumber() + 1);
                        erpWarehouseService.updateById(warehouse_id);
                    } else {
                        //判断如果去向的仓库没有这个物料那么加一个仓库物料
                        warehouse_id = new ErpWarehouse();
                        warehouse_id.setMaterielNumber(1);
                        warehouse_id.setCustomerId(erpSaleRepairRepair.getState());
                        warehouse_id.setMaterielName(erpSaleRepairRepair.getMaterielName());
                        warehouse_id.setMaterielBrand(erpSaleRepairRepair.getMaterielBrand());
                        warehouse_id.setMaterielSpecifications(erpSaleRepairRepair.getMaterielSpecifications());
                        warehouse_id.setMaterielPlace(erpSaleRepairRepair.getMaterielPlace());
                        warehouse_id.setMaterielState(erpSaleRepairRepair.getMaterielState());
                        warehouse_id.setMaterielUnit(erpSaleRepairRepair.getMaterielUnit());
                        warehouse_id.setWarehouseId(war_name.getId());
                        warehouse_id.setMaterielProductionId(erpMateriel.getMaterielProductionId());
                        warehouse_id.setMaterielTax(erpMateriel.getMaterielStoragePrice());
                        warehouse_id.setMaterielNotax(erpMateriel.getMaterielStoragePrice());
                        warehouse_id.setAllPrice(erpMateriel.getMaterielStoragePrice());
                        warehouse_id.setMaterielModel(erpMateriel.getMaterielModel());
                        erpWarehouseService.insert(warehouse_id);
                    }
                    //操作原物料 和 原仓库数据
                    if (erpMateriel != null) {
                        if (erpMateriel.getMaterielName().contains("轮胎(智能)")) {
                            ErpMateriel erpMaterielChuanganqi = erpMaterielService.selectById(erpMateriel.getCorrespondingId());
                            ErpWarehouse cangkuYuanchuanganqi = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", erpMaterielChuanganqi.getWarehouseId()).
                                    eq("materiel_name", erpMaterielChuanganqi.getMaterielName()).
                                    eq("materiel_brand", erpMaterielChuanganqi.getMaterielBrand()).
                                    eq("materiel_Specifications", erpMaterielChuanganqi.getMaterielSpecifications())
                                    .eq("materiel_place", erpMaterielChuanganqi.getMaterielPlace())
                                    .eq("materiel_state", erpMaterielChuanganqi.getMaterielState())
                                    .eq("materiel_unit", erpMaterielChuanganqi.getMaterielUnit()).eq("customer_id", erpMaterielChuanganqi.getCustomerId()));

                            //操作的原仓库物料 减一个物料
                            if (erpMaterielChuanganqi.getDelectType() == 1) {
                                if (cangkuYuan.getMaterielNumber() > 0) {
                                    cangkuYuanchuanganqi.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                                }
                            }
                            if (cangkuYuanchuanganqi != null && cangkuYuanchuanganqi.getAllPrice() - erpMaterielChuanganqi.getMaterielStoragePrice() > 0) {
                                cangkuYuanchuanganqi.setAllPrice(cangkuYuanchuanganqi.getAllPrice() - erpMaterielChuanganqi.getMaterielStoragePrice());
                            }
                            if(cangkuYuanchuanganqi != null){
                                erpWarehouseService.updateById(cangkuYuanchuanganqi);
                            }
                            erpMaterielChuanganqi.setWarehouseId(war_name.getId());
                            erpMaterielChuanganqi.setDelectType(0);
                            erpMaterielChuanganqi.setMaterielLoad(1);
                            erpMaterielService.updateById(erpMaterielChuanganqi);

                        }

                        //操作的原仓库物料 减一个物料
                        if (erpMateriel.getDelectType() == 1) {
                            if (cangkuYuan.getMaterielNumber() > 0) {
                                cangkuYuan.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                            }

                        }
                        erpWarehouseService.updateById(cangkuYuan);

                        //操作的物料 原物料减一个
                        erpMateriel.setWarehouseId(war_name.getId());
                        erpMateriel.setDelectType(1);
                        erpMaterielService.updateById(erpMateriel);
                    }
                } else {
                    if (erpMateriel != null) {
                        if (erpMateriel.getMaterielName().contains("轮胎(智能)")) {

                            ErpMateriel erpMaterielChuanganqi = erpMaterielService.selectById(erpMateriel.getCorrespondingId());
                            //传感器的仓库物料
                            ErpWarehouse cangkuYuanchuanganqi = erpWarehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("warehouse_id", erpMaterielChuanganqi.getWarehouseId()).
                                    eq("materiel_name", erpMaterielChuanganqi.getMaterielName()).
                                    eq("materiel_brand", erpMaterielChuanganqi.getMaterielBrand()).
                                    eq("materiel_Specifications", erpMaterielChuanganqi.getMaterielSpecifications())
                                    .eq("materiel_place", erpMaterielChuanganqi.getMaterielPlace())
                                    .eq("materiel_state", erpMaterielChuanganqi.getMaterielState())
                                    .eq("materiel_unit", erpMaterielChuanganqi.getMaterielUnit()).eq("customer_id", erpSaleRepairRepair.getState()));

                            //操作的原仓库物料 减一个物料
                            if (erpMaterielChuanganqi.getDelectType() == 1) {
                                if (cangkuYuan.getMaterielNumber() > 0) {
                                    cangkuYuanchuanganqi.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                                }
                            }
                            if (cangkuYuanchuanganqi.getAllPrice() - erpMaterielChuanganqi.getMaterielStoragePrice() > 0) {
                                cangkuYuanchuanganqi.setAllPrice(cangkuYuanchuanganqi.getAllPrice() - erpMaterielChuanganqi.getMaterielStoragePrice());
                            }
                            erpWarehouseService.updateById(cangkuYuanchuanganqi);

                            erpMaterielChuanganqi.setWarehouseId(war_name.getId());
                            erpMaterielChuanganqi.setKucunorliucun("留存");
                            erpMaterielChuanganqi.setDelectType(1);
                            //客户留存的客户id
                            erpMaterielChuanganqi.setCustomerId(erpSaleRepairRepair.getState());
                            erpMaterielService.updateById(erpMaterielChuanganqi);
                        }

                        //操作的原仓库物料 减一个物料
                        if (erpMateriel.getDelectType() == 1) {
                            if (cangkuYuan.getMaterielNumber() > 0) {
                                cangkuYuan.setMaterielNumber(cangkuYuan.getMaterielNumber() - 1);
                            }
                        }
                        erpWarehouseService.updateById(cangkuYuan);

                        //操作的物料 原物料减一个
                        erpMateriel.setWarehouseId(war_name.getId());
                        erpMateriel.setKucunorliucun("留存");

                        //客户留存的客户id
                        erpMateriel.setCustomerId(erpSaleRepairRepair.getState());
                        erpMaterielService.updateById(erpMateriel);
                    }
                }


            }
            if (objj != null&&objj.getTireId()!=0) {
                List<ErpTireSensor> ErpTireSensorList = erpTireSensorService.selectList(new EntityWrapper<ErpTireSensor>().eq("tire_id", objj.getTireId()));
                if (ErpTireSensorList.size() > 1) {
                    for (ErpTireSensor erpTireSensor : ErpTireSensorList) {
                        if (erpTireSensor.getErpcartirearchivesid() == null) {
                            erpTireSensorService.deleteById(erpTireSensor.getId());
                        }
                    }
                }
            }

            // 添加维修项目
            if (esrr != null && esrr.getState() == 0) {
                erpSaleRepairRepairService.deleteById(esrr);
            }

            erpSaleRepairRepair2 = new ErpSaleRepairRepair();
            erpSaleRepairRepair2.setMaterielNumber(1);
            erpSaleRepairRepair2.setMaterielSource(erpSaleRepairRepair.getMaterielSource());
            erpSaleRepairRepair2.setMaterielOutTo(erpSaleRepairRepair.getMaterielOutTo());
            erpSaleRepairRepair2.setMaterielBrand(erpMateriel.getMaterielBrand());
            erpSaleRepairRepair2.setMaterielModel(erpMateriel.getMaterielModel());
            erpSaleRepairRepair2.setMaterielName(erpMateriel.getMaterielName());
            erpSaleRepairRepair2.setMaterielSpecifications(erpMateriel.getMaterielSpecifications());
            erpSaleRepairRepair2.setMaterielModel(erpMateriel.getMaterielModel());
            erpSaleRepairRepair2.setMaterielPlace(erpMateriel.getMaterielPlace());
            erpSaleRepairRepair2.setMaterielState(erpMateriel.getMaterielState());
            erpSaleRepairRepair2.setMaterielUnit(erpMateriel.getMaterielUnit());
            erpSaleRepairRepair2.setMaterielProductionId(erpMateriel.getMaterielProductionId());
            erpSaleRepairRepair2.setMaterielPressure(erpSaleRepairRepair.getMaterielPressure());
            erpSaleRepairRepair2.setMaterielTaiyaid(erpSaleRepairRepair.getMaterielTaiyaid());
            erpSaleRepairRepair2.setMaterielPrice(erpSaleRepairRepair.getMaterielPrice());
            erpSaleRepairRepair2.setMaterielTotal(erpSaleRepairRepair.getMaterielTotal());
            erpSaleRepairRepair2.setDynamicBalancingLeft(erpSaleRepairRepair.getDynamicBalancingLeft());
            erpSaleRepairRepair2.setDynamicBalancingRight(erpSaleRepairRepair.getDynamicBalancingLeft());
            erpSaleRepairRepair2.setCarPlate(erpSaleRepairRepair.getCarPlate());
            erpSaleRepairRepair2.setErpMaterielId(erpMateriel.getId());
            erpSaleRepairRepair2.setState(1);
            erpSaleRepairRepair2.setCustomerId(erpSaleRepairRepair.getState());
            erpSaleRepairRepair2.setRepairRepair(erpSaleRepairRepair.getRepairRepair());

            erpSaleRepairRepairService.insert(erpSaleRepairRepair2);


            result.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("数据错误保存失败！");
        }

        return result;
    }

    //BI数据插入表
    public void insertBI(String wuliaoname, String CarNumber, String CarType1, String CarPlate, int custId, String CarWheel, int userId, String UserName, String MaterielProductionId) {

        ErpBidata bi = erpBidataMapper.selectByPlate(CarPlate);
        if (bi == null) {
            bi = new ErpBidata();
            bi.setAddDate(DataUtil.dateToStr(new Date()));
            bi.setCarNum(CarNumber);
            bi.setCarType(CarType1);
            bi.setCarPlate(CarPlate);
            bi.setCusId(custId);
            bi.setTirePressure(CarWheel);
            bi.setUserId(userId);
            bi.setUserName(UserName);
            erpBidataMapper.insert(bi);
        }
        ErpBidataList biList = erpBidataListMapper.selectByBiIdAndName(bi.getId(), wuliaoname);
        if (biList == null) {
            biList = new ErpBidataList();
            biList.setBiId(bi.getId());
            biList.setParameterId(482);
            biList.setParameterName(wuliaoname);
            biList.setParameterType(1);
            biList.setParameterValue(MaterielProductionId);
            erpBidataListMapper.insert(biList);
        } else {
            biList.setParameterValue(MaterielProductionId);
            erpBidataListMapper.updateById(biList);
        }

    }


    @RequestMapping("/delectXiaoshouweixiuShangPinLieBiao")
    @ApiOperation("根据id删除,销售维修-商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "销售维修商品id", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> delectXiaoshouweixiuShangPinLieBiao(Integer id) {

        Result<Integer> result = new Result<Integer>();
        try {
            result.setData(erpSaleRepairSaleService.deleteById(id) ? 1 : 0);
            result.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping("/addRepairOrder")
    @ApiOperation("添加物料维修")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "price", value = "价格", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "custId", value = "出库目标客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> addRepairOrder(ErpSaleRepairOrder repairOrder, String work_attn, String kuguanattn, String client_attn, String caiwuattn, String orderid,String car_number1) {
        Result<Integer> result = new Result<Integer>();
        String id = "XSWX" + IDUtils.genOrderId();
        ErpUser us=erpUserService.selectOne(new EntityWrapper<ErpUser>().eq("customer_id",repairOrder.getCustId()));

        int type = 1;
        try {
            //查询对应消息是否有这个信息
            ErpSystemMessage erpSystemMessage =erpSystemMessageService.selectOne(new EntityWrapper<ErpSystemMessage>().eq("send_content",orderid));
            if(orderid.equals("0")){
                ErpSystemMessage erpSystemMessage1=new ErpSystemMessage();
                erpSystemMessage1.setSendContent(id);
                erpSystemMessage1.setSendFlag("未读");
                erpSystemMessage1.setMessageType("销售维修");
                erpSystemMessage1.setCompanys(repairOrder.getCustomerName());
                erpSystemMessage1.setSendCustId(repairOrder.getCustId());
                erpSystemMessage1.setReceiveCustId(repairOrder.getCustId());
                erpSystemMessageService.updateById(erpSystemMessage1);
            }
            //判断
            if (work_attn!=null && kuguanattn!=null&& client_attn!=null&& caiwuattn!=null && orderid!=null&&!orderid.equals("0")) {
                ErpManagement em1 = erpManagementService.selectOne(new EntityWrapper<ErpManagement>().eq("work_id", orderid));
                em1.setWorkAttn(work_attn);
                em1.setClientAttn(client_attn);
                em1.setKuguanattn(kuguanattn);
                em1.setCaiwuattn(caiwuattn);
                em1.setType(1);
                erpManagementService.updateById(em1);
                erpSystemMessage.setSendFlag("已读");
                erpSystemMessageService.updateById(erpSystemMessage);
                return result;
            } else {
                type = 0;

            }


            Integer idph = 0;
            Map<String, Object> reslut = erpManagementService.selectTongYong("select max(work_id) work_id from erp_management where work_id like '" + id + "%'");
            if (reslut != null) {
                System.out.print(reslut.get("work_id").toString().substring(4, reslut.get("work_id").toString().length()));
                String numss = reslut.get("work_id").toString().substring(11, reslut.get("work_id").toString().length());
                idph = Integer.valueOf(numss);
                idph = idph + 1;
                id = id + idph.toString().substring(1, 6);
            } else {
                id = id + "00001";
            }
//            List<ErpSaleRepairOrder> erpRepairOrder = erpSaleRepairOrderService.selectList(new EntityWrapper<ErpSaleRepairOrder>().orderBy("id", false));
//            if (erpRepairOrder.size() > 0) {
//                ErpSaleRepairOrder cla = erpRepairOrder.get(0);
//                String str = erpRepairOrder.get(erpRepairOrder.size() - 1).getSaleRepairSheetId().substring(erpRepairOrder.get(erpRepairOrder.size() - 1).getSaleRepairSheetId().length() - 5);
//                str = IDUtils.addOne(str);
//                id += str;
//            } else {
//                id += "00001";
//            }
            repairOrder.setSaleRepairSheetId(id);


            //物料耗材批量修改单号
//            for (int i = 0; i < list.size(); i++) {
//                ErpHc erpHc =  list.get(i);
//                erpHc.setOrderId(a);
//            }

            String a = id;


            //物料耗材批量添加单号id
            result.setData(erpSaleRepairOrderService.insert(repairOrder) ? 1 : 0);


            List<ErpHc> list = erpHcService.selectList(new EntityWrapper<ErpHc>().eq("car_plate", repairOrder.getCarPlate()));   //1
            for (int i = 0; i < list.size(); i++) {
                ErpHc erpHc = list.get(i);
                if (erpHc.getOrderId() == null) {
                    erpHc.setOrderId(a);
                    erpHcService.updateById(erpHc);
                }
            }

//            list.parallelStream().forEach(hc->{
//                if (hc.getOrderId()==null){
//                    hc.setOrderId(a);
//                    erpHcService.updateById(hc);
//                }
//
//            });
//            for (int i = 0; i < list.size(); i++) {
//                ErpHc erpHc =  list.get(i);
//                erpHc.setOrderId(repairOrder.getId());
//                erpHcService.updateById(erpHc);
//
//            }


            //ErpSaleRepairGarage 车辆维修上面的,

            List<ErpSaleRepairGarage> erpSaleRepairGarages = erpSaleRepairGarageService.selectList(new EntityWrapper<ErpSaleRepairGarage>().eq("car_pai", repairOrder.getCarPlate()));
            erpSaleRepairGarages.parallelStream().forEach(erpSaleRepairGarage -> {
                if (erpSaleRepairGarage.getOrderId() == null) {
                    erpSaleRepairGarage.setOrderId(a);
                    erpSaleRepairGarageService.updateById(erpSaleRepairGarage);
                }
            });
// for (int i = 0; i < erpSaleRepairGarages.size(); i++) {
//                ErpSaleRepairGarage erpHc =  erpSaleRepairGarages.get(i);
//                erpHc.setOrderId(repairOrder.getId());
//                erpSaleRepairGarageService.updateById(erpHc);
//            }
//            erpSaleRepairGarageService.updateBatchById(erpSaleRepairGarages);

            //ErpSaleRepairSale  商品销售的  销售维修最上面
            double xiaoshouprice = 0.0;
            List<ErpSaleRepairSale> erpSaleRepairSales = erpSaleRepairSaleService.selectList(new EntityWrapper<ErpSaleRepairSale>().eq("car_pai", repairOrder.getCarPlate()));
            for (int i = 0; i < erpSaleRepairSales.size(); i++) {
                ErpSaleRepairSale erpSaleRepairSale = erpSaleRepairSales.get(i);
                if (erpSaleRepairSale.getOrderId().equals("1")) {
                    //添加轮胎档案工作表
                    ErpTireArchivesWork erpTireArchivesWork = new ErpTireArchivesWork();
                    erpTireArchivesWork.setTireId(erpSaleRepairSale.getErpMaterielId());
                    erpTireArchivesWork.setWorkSheetId(a);
                    erpTireArchivesWork.setWorkServiceSector(repairOrder.getCustomerName());
                    erpTireArchivesWork.setTireLocation(erpSaleRepairSale.getMaterielOutTo());
                    erpTireArchivesWork.setWorkMileage(repairOrder.getSaleRepairMileage());
                    erpTireArchivesWork.setWorkProject("销售/维修");
                    erpTireArchivesWork.setWorkTireState(erpSaleRepairSale.getMaterielPlace());
                    erpTireArchivesWork.setTireCost(erpSaleRepairSale.getMaterielPrice());
                    erpTireArchivesWork.setTireState(erpSaleRepairSale.getMaterielPlace());
                    erpTireArchivesWork.setCustomerId(repairOrder.getCustomerId());
                    erpTireArchivesWork.setCostType(2);
                    erpTireArchivesWork.setCalculatingMileage(1);
                    erpTireArchivesWorkService.insert(erpTireArchivesWork);


                    erpSaleRepairSale.setOrderId(a);
                    xiaoshouprice += erpSaleRepairSale.getMaterielTotal();
                    erpSaleRepairSaleService.updateById(erpSaleRepairSale);

                    ErpMateriel erpMateriel = erpMaterielService.selectById(erpSaleRepairSale.getErpMaterielId());
                    String souls = erpCustomerWarehouseService.selectById(erpSaleRepairSale.getWarehouseId()).getWarName();
                    List<ErpUser> erpUserList = erpUserService.selectList(new EntityWrapper<ErpUser>().eq("customer_id", erpSaleRepairSale.getCustomerId()));
                    if (souls.contains("存") && erpSaleRepairSale.getMaterielOutTo().contains("存")) {
                        //出库操作
                        ErpMaterielFlowSummary erpMaterielFlowSummary = new ErpMaterielFlowSummary();
                        erpMaterielFlowSummary.setWarehouseId(erpMateriel.getWarehouseId());
                        erpMaterielFlowSummary.setMaterielId(erpMateriel.getId());
                        erpMaterielFlowSummary.setWorkSheetId(a);
                        erpMaterielFlowSummary.setMaterielOutflowNumber(1);
                        erpMaterielFlowSummary.setMaterielOutflowPrice(erpSaleRepairSale.getMaterielPrice());
                        erpMaterielFlowSummary.setIncludingTax(1);
                        //出库状态
                        erpMaterielFlowSummary.setMaterielFlowType(1);
                        if (erpSaleRepairSale.getMaterielOutTo().contains("库存")) {
                            erpMaterielFlowSummary.setMaterielRetained(0);
                        } else {
                            erpMaterielFlowSummary.setMaterielRetained(1);
                        }
                        erpMaterielFlowSummary.setFlowSubject("销售维修");
                        erpMaterielFlowSummary.setMaterielWhereabouts(erpSaleRepairSale.getMaterielOutTo());
                        erpMaterielFlowSummary.setMaterielSource(souls);
                        erpMaterielFlowSummary.setCarplate(erpSaleRepairSale.getCarPai());
                        erpMaterielFlowSummary.setMaterielOutflowNumber(1);

                        erpMaterielFlowSummary.setFlowAttn(erpUserList.get(0).getRealName());
                        erpMaterielFlowSummary.setOutWarehouseRetauned(1);
                        if (souls.contains("库存") || erpSaleRepairSale.getMaterielOutTo().contains("留存")) {
                            erpMaterielFlowSummary.setOutWarehouseId(erpMateriel.getWarehouseId());
                        }
                        erpMaterielFlowSummary.setMaterielProductionId(erpMateriel.getMaterielProductionId());
                        erpMaterielFlowSummaryService.insert(erpMaterielFlowSummary);

                        //入库操作
                        erpMaterielFlowSummary.setWarehouseId(erpMateriel.getWarehouseId());
                        erpMaterielFlowSummary.setMaterielId(erpMateriel.getId());
                        erpMaterielFlowSummary.setWorkSheetId(a);
                        erpMaterielFlowSummary.setMaterielStorageNumber(1);
                        erpMaterielFlowSummary.setMaterielStoragePrice(erpSaleRepairSale.getMaterielPrice());
                        erpMaterielFlowSummary.setIncludingTax(1);
                        //入库状态
                        erpMaterielFlowSummary.setMaterielFlowType(0);
                        if (erpSaleRepairSale.getMaterielOutTo().contains("库存")) {
                            erpMaterielFlowSummary.setMaterielRetained(0);
                        } else {
                            erpMaterielFlowSummary.setMaterielRetained(1);
                        }
                        erpMaterielFlowSummary.setFlowSubject("销售维修");
                        erpMaterielFlowSummary.setMaterielWhereabouts(erpSaleRepairSale.getMaterielOutTo());
                        erpMaterielFlowSummary.setMaterielSource(souls);
                        erpMaterielFlowSummary.setCarplate(erpSaleRepairSale.getCarPai());

                        erpMaterielFlowSummary.setFlowAttn(erpUserList.get(0).getRealName());
                        erpMaterielFlowSummary.setOutWarehouseRetauned(1);
                        if (souls.contains("库存") || erpSaleRepairSale.getMaterielOutTo().contains("留存")) {
                            erpMaterielFlowSummary.setOutWarehouseId(erpMateriel.getWarehouseId());
                        }
                        erpMaterielFlowSummary.setMaterielProductionId(erpMateriel.getMaterielProductionId());
                        erpMaterielFlowSummaryService.insert(erpMaterielFlowSummary);
                    } else if (souls.contains("存") && !erpSaleRepairSale.getMaterielOutTo().contains("存")) {
                        //出库操作
                        ErpMaterielFlowSummary erpMaterielFlowSummary = new ErpMaterielFlowSummary();
                        erpMaterielFlowSummary.setWarehouseId(erpMateriel.getWarehouseId());
                        erpMaterielFlowSummary.setMaterielId(erpMateriel.getId());
                        erpMaterielFlowSummary.setWorkSheetId(a);
                        erpMaterielFlowSummary.setMaterielOutflowNumber(1);
                        erpMaterielFlowSummary.setMaterielOutflowPrice(erpSaleRepairSale.getMaterielPrice());
                        erpMaterielFlowSummary.setIncludingTax(1);
                        //出库状态
                        erpMaterielFlowSummary.setMaterielFlowType(1);
                        if (erpSaleRepairSale.getMaterielOutTo().contains("库存")) {
                            erpMaterielFlowSummary.setMaterielRetained(0);
                        } else {
                            erpMaterielFlowSummary.setMaterielRetained(1);
                        }
                        erpMaterielFlowSummary.setFlowSubject("销售维修");
                        erpMaterielFlowSummary.setMaterielWhereabouts(erpSaleRepairSale.getMaterielOutTo());
                        erpMaterielFlowSummary.setMaterielSource(souls);
                        erpMaterielFlowSummary.setCarplate(erpSaleRepairSale.getCarPai());


                        erpMaterielFlowSummary.setFlowAttn(erpUserList.get(0).getRealName());
                        erpMaterielFlowSummary.setOutWarehouseRetauned(1);
                        if (souls.contains("库存") || erpSaleRepairSale.getMaterielOutTo().contains("留存")) {
                            erpMaterielFlowSummary.setOutWarehouseId(erpMateriel.getWarehouseId());
                        }
                        erpMaterielFlowSummary.setMaterielProductionId(erpMateriel.getMaterielProductionId());
                        erpMaterielFlowSummaryService.insert(erpMaterielFlowSummary);
                    } else if (!souls.contains("存") && erpSaleRepairSale.getMaterielOutTo().contains("存")) {

                        ErpMaterielFlowSummary erpMaterielFlowSummary = new ErpMaterielFlowSummary();
                        //入库操作
                        erpMaterielFlowSummary.setWarehouseId(erpMateriel.getWarehouseId());
                        erpMaterielFlowSummary.setMaterielId(erpMateriel.getId());
                        erpMaterielFlowSummary.setWorkSheetId(a);
                        erpMaterielFlowSummary.setMaterielStorageNumber(1);
                        erpMaterielFlowSummary.setMaterielStoragePrice(erpSaleRepairSale.getMaterielPrice());
                        erpMaterielFlowSummary.setIncludingTax(1);
                        erpMaterielFlowSummary.setMaterielOutflowNumber(1);
                        //入库状态
                        erpMaterielFlowSummary.setMaterielFlowType(0);
                        if (erpSaleRepairSale.getMaterielOutTo().contains("库存")) {
                            erpMaterielFlowSummary.setMaterielRetained(0);
                        } else {
                            erpMaterielFlowSummary.setMaterielRetained(1);
                        }
                        erpMaterielFlowSummary.setFlowSubject("销售维修");
                        erpMaterielFlowSummary.setMaterielWhereabouts(erpSaleRepairSale.getMaterielOutTo());
                        erpMaterielFlowSummary.setMaterielSource(souls);
                        erpMaterielFlowSummary.setCarplate(erpSaleRepairSale.getCarPai());
                        erpMaterielFlowSummary.setMaterielOutflowPrice(erpSaleRepairSale.getMaterielPrice());
                        erpMaterielFlowSummary.setFlowAttn(erpUserList.get(0).getRealName());
                        erpMaterielFlowSummary.setOutWarehouseRetauned(1);
                        if (souls.contains("库存") || erpSaleRepairSale.getMaterielOutTo().contains("留存")) {
                            erpMaterielFlowSummary.setOutWarehouseId(erpMateriel.getWarehouseId());
                        }
                        erpMaterielFlowSummary.setMaterielProductionId(erpMateriel.getMaterielProductionId());
                        erpMaterielFlowSummaryService.insert(erpMaterielFlowSummary);
                    }
                }
            }

//            erpSaleRepairSales.parallelStream().forEach(erpSaleRepairGarage -> {
//                if (erpSaleRepairGarage.getOrderId()==null){
//                    erpSaleRepairGarage.setOrderId(a);
//                    erpSaleRepairSaleService.updateById(erpSaleRepairGarage);
//                }
//            });
            //            for (int i = 0; i < erpSaleRepairSales.size(); i++) {
//                ErpSaleRepairSale erpSaleRepairSale =  erpSaleRepairSales.get(i);
//                erpSaleRepairSale.setOrderId(repairOrder.getId());
//                erpSaleRepairSaleService.updateById(erpSaleRepairSale);
//            }
//            erpSaleRepairSaleService.updateBatchById(erpSaleRepairSales);

            //物料维修  ErpSaleRepairRepair   上面胎位
            double weixiuprice = 0.0;
            List<ErpSaleRepairRepair> erpSaleRepairRepairs = erpSaleRepairRepairService.selectList(new EntityWrapper<ErpSaleRepairRepair>().eq("car_plate", repairOrder.getCarPlate()));
            for (int i = 0; i < erpSaleRepairRepairs.size(); i++) {
                ErpSaleRepairRepair erpSaleRepairRepair = erpSaleRepairRepairs.get(i);
                if (erpSaleRepairRepair.getOrderId() == null) {
                    ErpMateriel erpMateriel = erpMaterielService.selectById(erpSaleRepairRepair.getErpMaterielId());
                    List<ErpUser> erpUserList = erpUserService.selectList(new EntityWrapper<ErpUser>().eq("customer_id", erpSaleRepairRepair.getCustomerId()));
                    if (erpSaleRepairRepair.getMaterielSource().contains("存") && erpSaleRepairRepair.getMaterielOutTo().contains("存")) {
                        //出库操作
                        ErpMaterielFlowSummary erpMaterielFlowSummary = new ErpMaterielFlowSummary();
                        erpMaterielFlowSummary.setWarehouseId(erpMateriel.getWarehouseId());
                        erpMaterielFlowSummary.setMaterielId(erpMateriel.getId());
                        erpMaterielFlowSummary.setWorkSheetId(a);
                        erpMaterielFlowSummary.setMaterielOutflowNumber(1);
                        erpMaterielFlowSummary.setMaterielOutflowPrice(erpSaleRepairRepair.getMaterielPrice());
                        erpMaterielFlowSummary.setIncludingTax(1);
                        //出库状态
                        erpMaterielFlowSummary.setMaterielFlowType(1);
                        if (erpSaleRepairRepair.getMaterielOutTo().contains("库存")) {
                            erpMaterielFlowSummary.setMaterielRetained(0);
                        } else {
                            erpMaterielFlowSummary.setMaterielRetained(1);
                        }
                        erpMaterielFlowSummary.setFlowSubject("销售维修");
                        erpMaterielFlowSummary.setMaterielWhereabouts(erpSaleRepairRepair.getMaterielOutTo());
                        erpMaterielFlowSummary.setMaterielSource(erpSaleRepairRepair.getMaterielSource());
                        erpMaterielFlowSummary.setCarplate(erpSaleRepairRepair.getCarPlate());


                        erpMaterielFlowSummary.setFlowAttn(erpUserList.get(0).getRealName());
                        erpMaterielFlowSummary.setOutWarehouseRetauned(1);
                        if (erpSaleRepairRepair.getMaterielOutTo().contains("库存") || erpSaleRepairRepair.getMaterielOutTo().contains("留存")) {
                            erpMaterielFlowSummary.setOutWarehouseId(erpMateriel.getWarehouseId());
                        }
                        erpMaterielFlowSummary.setMaterielProductionId(erpMateriel.getMaterielProductionId());
                        erpMaterielFlowSummaryService.insert(erpMaterielFlowSummary);

                        //入库操作
                        erpMaterielFlowSummary.setWarehouseId(erpMateriel.getWarehouseId());
                        erpMaterielFlowSummary.setMaterielId(erpMateriel.getId());
                        erpMaterielFlowSummary.setWorkSheetId(a);
                        erpMaterielFlowSummary.setMaterielStorageNumber(1);
                        erpMaterielFlowSummary.setMaterielStoragePrice(erpSaleRepairRepair.getMaterielPrice());
                        erpMaterielFlowSummary.setIncludingTax(1);
                        //入库状态
                        erpMaterielFlowSummary.setMaterielFlowType(0);
                        if (erpSaleRepairRepair.getMaterielOutTo().contains("库存")) {
                            erpMaterielFlowSummary.setMaterielRetained(0);
                        } else {
                            erpMaterielFlowSummary.setMaterielRetained(1);
                        }
                        erpMaterielFlowSummary.setFlowSubject("销售维修");
                        erpMaterielFlowSummary.setMaterielWhereabouts(erpSaleRepairRepair.getMaterielOutTo());
                        erpMaterielFlowSummary.setMaterielSource(erpSaleRepairRepair.getMaterielSource());
                        erpMaterielFlowSummary.setCarplate(erpSaleRepairRepair.getCarPlate());

                        erpMaterielFlowSummary.setFlowAttn(erpUserList.get(0).getRealName());
                        erpMaterielFlowSummary.setOutWarehouseRetauned(1);
                        if (erpSaleRepairRepair.getMaterielOutTo().contains("库存") || erpSaleRepairRepair.getMaterielOutTo().contains("留存")) {
                            erpMaterielFlowSummary.setOutWarehouseId(erpMateriel.getWarehouseId());
                        }
                        erpMaterielFlowSummary.setMaterielProductionId(erpMateriel.getMaterielProductionId());
                        erpMaterielFlowSummaryService.insert(erpMaterielFlowSummary);
                    } else if (erpSaleRepairRepair.getMaterielSource().contains("存") && !erpSaleRepairRepair.getMaterielOutTo().contains("存")) {
                        //出库操作
                        ErpMaterielFlowSummary erpMaterielFlowSummary = new ErpMaterielFlowSummary();
                        erpMaterielFlowSummary.setWarehouseId(erpMateriel.getWarehouseId());
                        erpMaterielFlowSummary.setMaterielId(erpMateriel.getId());
                        erpMaterielFlowSummary.setWorkSheetId(a);
                        erpMaterielFlowSummary.setMaterielOutflowNumber(1);
                        erpMaterielFlowSummary.setMaterielOutflowPrice(erpSaleRepairRepair.getMaterielPrice());
                        erpMaterielFlowSummary.setIncludingTax(1);
                        //出库状态
                        erpMaterielFlowSummary.setMaterielFlowType(1);
                        if (erpSaleRepairRepair.getMaterielOutTo().contains("库存")) {
                            erpMaterielFlowSummary.setMaterielRetained(0);
                        } else {
                            erpMaterielFlowSummary.setMaterielRetained(1);
                        }
                        erpMaterielFlowSummary.setFlowSubject("销售维修");
                        erpMaterielFlowSummary.setMaterielWhereabouts(erpSaleRepairRepair.getMaterielOutTo());
                        erpMaterielFlowSummary.setMaterielSource(erpSaleRepairRepair.getMaterielSource());
                        erpMaterielFlowSummary.setCarplate(erpSaleRepairRepair.getCarPlate());
                        erpMaterielFlowSummary.setMaterielStoragePrice(erpSaleRepairRepair.getMaterielPrice());

                        erpMaterielFlowSummary.setFlowAttn(erpUserList.get(0).getRealName());
                        erpMaterielFlowSummary.setOutWarehouseRetauned(1);
                        if (erpSaleRepairRepair.getMaterielOutTo().contains("库存") || erpSaleRepairRepair.getMaterielOutTo().contains("留存")) {
                            erpMaterielFlowSummary.setOutWarehouseId(erpMateriel.getWarehouseId());
                        }
                        erpMaterielFlowSummary.setMaterielProductionId(erpMateriel.getMaterielProductionId());
                        erpMaterielFlowSummaryService.insert(erpMaterielFlowSummary);
                    } else if (!erpSaleRepairRepair.getMaterielSource().contains("存") && erpSaleRepairRepair.getMaterielOutTo().contains("存")) {

                        ErpMaterielFlowSummary erpMaterielFlowSummary = new ErpMaterielFlowSummary();
                        //入库操作
                        erpMaterielFlowSummary.setWarehouseId(erpMateriel.getWarehouseId());
                        erpMaterielFlowSummary.setMaterielId(erpMateriel.getId());
                        erpMaterielFlowSummary.setWorkSheetId(a);
                        erpMaterielFlowSummary.setMaterielStorageNumber(1);
                        erpMaterielFlowSummary.setMaterielStoragePrice(erpSaleRepairRepair.getMaterielPrice());
                        erpMaterielFlowSummary.setMaterielOutflowNumber(1);
                        erpMaterielFlowSummary.setMaterielOutflowPrice(erpSaleRepairRepair.getMaterielPrice());
                        erpMaterielFlowSummary.setIncludingTax(1);
                        //入库状态
                        erpMaterielFlowSummary.setMaterielFlowType(0);
                        if (erpSaleRepairRepair.getMaterielOutTo().contains("库存")) {
                            erpMaterielFlowSummary.setMaterielRetained(0);
                        } else {
                            erpMaterielFlowSummary.setMaterielRetained(1);
                        }
                        erpMaterielFlowSummary.setFlowSubject("销售维修");
                        erpMaterielFlowSummary.setMaterielWhereabouts(erpSaleRepairRepair.getMaterielOutTo());
                        erpMaterielFlowSummary.setMaterielSource(erpSaleRepairRepair.getMaterielSource());
                        erpMaterielFlowSummary.setCarplate(erpSaleRepairRepair.getCarPlate());

                        erpMaterielFlowSummary.setFlowAttn(erpUserList.get(0).getRealName());
                        erpMaterielFlowSummary.setOutWarehouseRetauned(1);
                        if (erpSaleRepairRepair.getMaterielOutTo().contains("库存") || erpSaleRepairRepair.getMaterielOutTo().contains("留存")) {
                            erpMaterielFlowSummary.setOutWarehouseId(erpMateriel.getWarehouseId());
                        }
                        erpMaterielFlowSummary.setMaterielProductionId(erpMateriel.getMaterielProductionId());
                        erpMaterielFlowSummaryService.insert(erpMaterielFlowSummary);
                    }
                    erpSaleRepairRepair.setOrderId(a);
                    weixiuprice += erpSaleRepairRepair.getMaterielTotal();
                    erpSaleRepairRepairService.updateById(erpSaleRepairRepair);
                }
            }

//            List<ErpMaterielFlowSummary> erpMaterielFlowSummaryList = erpMaterielFlowSummaryService.selectList(new EntityWrapper<ErpMaterielFlowSummary>().eq("carplate", repairOrder.getCarPlate()));
//            for (int i = 0; i < erpMaterielFlowSummaryList.size(); i++) {
//                ErpMaterielFlowSummary erpMaterielFlowSummary = erpMaterielFlowSummaryList.get(i);
//                if (erpMaterielFlowSummary.getWorkSheetId() == null) {
//                    erpMaterielFlowSummary.setWorkSheetId(a);
//                    erpMaterielFlowSummaryService.updateById(erpMaterielFlowSummary);
//                }
//            }
//            erpSaleRepairRepairs.parallelStream().forEach(erpSaleRepairGarage -> {
//                if (erpSaleRepairGarage.getOrderId()==null){
//                    erpSaleRepairGarage.setOrderId(a);
//                    erpSaleRepairRepairService.updateById(erpSaleRepairGarage);
//                }
//            });
//            for (int i = 0; i < erpSaleRepairRepairs.size(); i++) {
//                ErpSaleRepairRepair erpSaleRepairSale =  erpSaleRepairRepairs.get(i);
//                erpSaleRepairSale.setOrderId(repairOrder.getId());
//                erpSaleRepairRepairService.updateById(erpSaleRepairSale);
//            }
//            erpSaleRepairRepairService.updateBatchById(erpSaleRepairRepairs);


            //物料维修  维修下面的, 维修耗材

            List<ErpSaleRepairSupplies> erpSaleRepairSupplies = erpSaleRepairSuppliesService.selectList(new EntityWrapper<ErpSaleRepairSupplies>().eq("car_plate", repairOrder.getCarPlate()));
            for (int i = 0; i < erpSaleRepairSupplies.size(); i++) {
                ErpSaleRepairSupplies saleRepairSupplies = erpSaleRepairSupplies.get(i);
                if (saleRepairSupplies.getSaleRepairRepairId() == null) {
                    saleRepairSupplies.setSaleRepairRepairId(a);
                    erpSaleRepairSuppliesService.updateById(saleRepairSupplies);
                }
            }
            if (orderid.equals("0")) {
                ErpManagement em = new ErpManagement();
                em.setDt(repairOrder.getSaleRepairDate());
                em.setWorkId(a);
                em.setClientName(repairOrder.getCustomerName());
                em.setCarNumber(repairOrder.getCarPlate());
                em.setWorkProject("销售/维修");
                em.setPurchase(0.0);
                em.setSail(xiaoshouprice);
                em.setErpRepair(weixiuprice);
                em.setMaintian(0.0);
                if(repairOrder.getSaleRepairPaymentMethod().equals("应收账款")){
                    em.setAccounts(xiaoshouprice + weixiuprice);
                }else{
                    em.setCope(xiaoshouprice + weixiuprice);
                }
                em.setCarNumber1(car_number1);
                em.setCash(xiaoshouprice + weixiuprice);
                em.setWorkAttn(work_attn);
                em.setClientAttn(client_attn);
                em.setKuguanattn(kuguanattn);
                em.setCaiwuattn(caiwuattn);
                em.setAttribute1(repairOrder.getSaleRepairMileage() + "");
                em.setWorkPlace(repairOrder.getCustomerName());
                em.setCustomerId(repairOrder.getCustomerId());
                em.setAccounts(xiaoshouprice + weixiuprice);
                em.setType(type);
                erpManagementService.insert(em);

                //添加订单标识表
                ErpSheetId erpSheetId = new ErpSheetId();
                erpSheetId.setSheetDate(new Date());
                erpSheetId.setSheetId(a);
                erpSheetId.setSheetComplete(1);
                erpSheetId.setSheetSort(1);
                erpSheetId.setSheetType("XSWX");
                erpSheetIdService.insert(erpSheetId);
            }else{
                ErpManagement em1 = erpManagementService.selectOne(new EntityWrapper<ErpManagement>().eq("work_id", orderid));
                em1.setWorkAttn(work_attn);
                em1.setClientAttn(client_attn);
                em1.setKuguanattn(kuguanattn);
                em1.setCaiwuattn(caiwuattn);
                em1.setType(0);
                erpManagementService.updateById(em1);
            }
//            erpSaleRepairSupplies.parallelStream().forEach(erpSaleRepairGarage -> {
//                if (erpSaleRepairGarage.getSaleRepairRepairId()==null){
//                    erpSaleRepairGarage.setSaleRepairRepairId(a);
//                    erpSaleRepairSuppliesService.updateById(erpSaleRepairGarage);
//                }
//            });
            //            for (int i = 0; i < erpSaleRepairSupplies.size(); i++) {
//                ErpSaleRepairSupplies erpSaleRepairSale =  erpSaleRepairSupplies.get(i);
//                erpSaleRepairSale.setSaleRepairRepairId(repairOrder.getId());
//                erpSaleRepairSuppliesService.updateById(erpSaleRepairSale);
//            }
//            erpSaleRepairRepairService.updateBatchById(erpSaleRepairRepairs);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }

}